Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Problema illuminazione pixel per pixel con GLSL (OpenGL Shading Language)
Forum - C/C++ - Problema illuminazione pixel per pixel con GLSL (OpenGL Shading Language)

Avatar
carlduke (Member)
Pro


Messaggi: 153
Iscritto: 29/01/2011

Segnala al moderatore
Postato alle 19:20
Mercoledė, 02/02/2011
Salve a tutti, ho il seguente problema :
Sto imparando il linguaggio GLSL e come ultimo stavo applicando l'illuminazione per pixel ma sembra che funzioni solo con i modelli forniti da glut (glutSolidCube(),glutSolidTeaPot()..ecc) mentre con qualsiasi altro modello, caricato da file .obj, oppure scritto da codice, come un cubo nel mio caso, l'illuminazione č uguale per tutte le facce.
Posto uno screen per essere piu chiaro (il cubo a sinistra lo disegno con drawCube() mentre quello a destra con glutSolidCube() ).

Questo č il main.cpp

Codice sorgente - presumibilmente C++

  1. #include "Shader.h"
  2. #include <stdlib.h>
  3.  
  4. Shader shader;
  5.  
  6. float angle = 0.0;
  7.  
  8. GLfloat lx = 0.0;
  9. GLfloat ly = 1.0;
  10. GLfloat lz = 1.0;
  11. GLfloat lw = 0.0;
  12.  
  13. void init(void) {
  14.     glEnable(GL_DEPTH_TEST);
  15.     glDepthFunc(GL_LESS);
  16.  
  17.     glEnable(GL_LIGHTING);
  18.     glEnable(GL_LIGHT0);
  19.  
  20.     shader.init("shader.vert", "shader.frag");
  21. }
  22.  
  23. void drawCube(){
  24.     glBegin(GL_QUADS);
  25.        glVertex3f(1.0,-1.0,-1.0);
  26.        glVertex3f(1.0,-1.0,1.0);
  27.        glVertex3f(-1.0,-1.0,1.0);
  28.        glVertex3f(-1.0,-1.0,-1.0);
  29.  
  30.        glVertex3f(1.0,1.0,-1.0);
  31.        glVertex3f(1.0,1.0,1.0);
  32.        glVertex3f(-1.0,1.0,1.0);
  33.        glVertex3f(-1.0,1.0,-1.0);
  34.  
  35.        glVertex3f(-1.0,-1.0,-1.0);
  36.        glVertex3f(-1.0,1.0,-1.0);
  37.        glVertex3f(1.0,1.0,-1.0);
  38.        glVertex3f(1.0,-1.0,-1.0);
  39.  
  40.        glVertex3f(-1.0,-1.0,1.0);
  41.        glVertex3f(-1.0,1.0,1.0);
  42.        glVertex3f(1.0,1.0,1.0);
  43.        glVertex3f(1.0,-1.0,1.0);
  44.  
  45.        glVertex3f(1.0,1.0,1.0);
  46.        glVertex3f(1.0,-1.0,1.0);
  47.        glVertex3f(1.0,-1.0,-1.0);
  48.        glVertex3f(1.0,1.0,-1.0);
  49.  
  50.        glVertex3f(-1.0,1.0,1.0);
  51.        glVertex3f(-1.0,-1.0,1.0);
  52.        glVertex3f(-1.0,-1.0,-1.0);
  53.        glVertex3f(-1.0,1.0,-1.0);
  54.  
  55.     glEnd();
  56. }
  57.  
  58. void cube(){
  59.     glRotatef(angle, 1.0, 0.0, 0.0); //rotate on the x axis
  60.     glRotatef(angle, 0.0, 1.0, 0.0); //rotate on the y axis
  61.     glRotatef(angle, 0.0, 0.0, 1.0); //rotate on the z axis
  62.  
  63.     GLfloat mShininess[] = {50};
  64.  
  65.     GLfloat DiffuseMaterial[] = {1.0, 0.0, 0.0};
  66.     GLfloat AmbientMaterial[] = {0.0, 0.0, 0.0};
  67.     GLfloat SpecularMaterial[] = {1.0, 1.0, 1.0};
  68.  
  69.     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, DiffuseMaterial);
  70.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, AmbientMaterial);
  71.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, SpecularMaterial);
  72.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mShininess);
  73.  
  74.     drawCube();
  75.     //glutSolidCube(2.0);
  76.     //glutSolidTeapot(2.0);
  77. }
  78.  
  79. void setLighting(void) {
  80.     GLfloat DiffuseLight[] = {1.0, 1.0, 1.0};
  81.     GLfloat AmbientLight[] = {0.2, 0.2, 0.2};
  82.     GLfloat SpecularLight[] = {1.0, 1.0, 1.0};
  83.  
  84.     glLightfv (GL_LIGHT0, GL_SPECULAR, SpecularLight);
  85.     glLightfv (GL_LIGHT0, GL_DIFFUSE, DiffuseLight);
  86.     glLightfv (GL_LIGHT0, GL_AMBIENT, AmbientLight);
  87.  
  88.     GLfloat LightPosition[] = {lx, ly, lz, lw}; //set the LightPosition to the specified values
  89.  
  90.     glLightfv (GL_LIGHT0, GL_POSITION, LightPosition); //change the light accordingly
  91. }
  92.  
  93. void display (void) {
  94.     glClearColor (0.0,0.0,0.0,1.0);
  95.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  96.     glLoadIdentity();
  97.     gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  98.  
  99.     setLighting();
  100.  
  101.     shader.bind();
  102.     cube();
  103.     shader.unbind();
  104.  
  105.     glutSwapBuffers();
  106.     angle += 0.02f;
  107. }
  108.  
  109. void reshape (int w, int h) {
  110.     glViewport (0, 0, (GLsizei)w, (GLsizei)h);
  111.     glMatrixMode (GL_PROJECTION);
  112.     glLoadIdentity ();
  113.     gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
  114.     glMatrixMode (GL_MODELVIEW);
  115. }
  116.  
  117. int main (int argc, char **argv) {
  118.     glutInit(&argc, argv);
  119.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); //set up the double buffering
  120.     glutInitWindowSize(800, 600);
  121.     glutInitWindowPosition(100, 100);
  122.     glutCreateWindow("A basic OpenGL Window");
  123.     glewInit();
  124.     glutDisplayFunc(display);
  125.     glutIdleFunc(display);
  126.  
  127.     glutReshapeFunc(reshape);
  128.  
  129.     init();
  130.  
  131.     glutMainLoop();
  132.  
  133.     return 0;
  134. }



Questo č il file Shader.h

Codice sorgente - presumibilmente C++

  1. #ifndef SHADER_H
  2.  #define SHADER_H
  3.  
  4. #include <stdlib.h>
  5. #include <GL/glew.h>
  6. #include <GL/glut.h>
  7. #include <string>
  8. #include "string_extra.h"
  9.  
  10. class Shader{
  11.    public :
  12.      Shader();
  13.      Shader(const char* vsFile,const char* fsFile);
  14.      ~Shader();
  15.  
  16.      void init(const char* vsFile,const char* fsFile);
  17.  
  18.      void bind();
  19.      void unbind();
  20.  
  21.      unsigned int id();
  22.  
  23.    protected :
  24.      unsigned int shader_id;
  25.      unsigned int shader_vp;
  26.      unsigned int shader_fp;
  27.  
  28. };
  29.  
  30. #endif



questo č Shader.cpp (dove textFileRead() definita nell'header string_extra.h č una funzione che restituisce il contenuto di un file e error č una semplice classe per le eccezioni)

Codice sorgente - presumibilmente C++

  1. #include "Shader.h"
  2. #include <string>
  3. #include <stdlib.h>
  4. #include "string_extra.h"
  5. #include "Error.h"
  6.  
  7.  
  8. static void validateShader(GLuint shader,const char* file = 0){
  9.   const unsigned int BUFFER_SIZE = 512;
  10.   char buffer[BUFFER_SIZE];
  11.   memset(buffer,0,BUFFER_SIZE);
  12.   GLsizei length = 0;
  13.  
  14.   glGetShaderInfoLog(shader,BUFFER_SIZE,&length,buffer);
  15.   if(length > 0){
  16.     cerr<<"Shader "<<shader<<" ("<<(file?file:" ")<<") compile error: "<<buffer<<endl;
  17.   }
  18. }
  19.  
  20.  
  21. static void validateProgram(GLuint program){
  22.     const unsigned int BUFFER_SIZE = 512;
  23.     char buffer[BUFFER_SIZE];
  24.     memset(buffer,0,BUFFER_SIZE);
  25.     GLsizei length = 0;
  26.  
  27.     memset(buffer,0,BUFFER_SIZE);
  28.     glGetProgramInfoLog(program, BUFFER_SIZE, &length, buffer);
  29.     if (length > 0)
  30.         cerr<<"Program "<<program<<" link error: "<<buffer<< endl;
  31.  
  32.     glValidateProgram(program);
  33.     GLint status;
  34.     glGetProgramiv(program,GL_VALIDATE_STATUS,&status);
  35.     if (status == GL_FALSE)
  36.      cerr<<"Error validating shader "<<program<<endl;
  37. }
  38.  
  39. Shader::Shader(){
  40.  
  41. }
  42.  
  43. Shader::Shader(const char* vsFile,const char* fsFile){
  44.    init(vsFile,fsFile);
  45. }
  46.  
  47. Shader::~Shader(){
  48.    glDetachShader(shader_id,shader_fp);
  49.    glDetachShader(shader_id,shader_vp);
  50.  
  51.    glDeleteShader(shader_fp);
  52.    glDeleteShader(shader_vp);
  53.    glDeleteProgram(shader_id);
  54. }
  55.  
  56. void Shader::init(const char* vsFile,const char* fsFile){
  57.    shader_vp = glCreateShader(GL_VERTEX_SHADER);
  58.    shader_fp = glCreateShader(GL_FRAGMENT_SHADER);
  59.  
  60.    const char* vsText = textFileRead(vsFile);
  61.    const char* fsText = textFileRead(fsFile);
  62.  
  63.   try{
  64.  
  65.  
  66.    if(vsText == NULL || fsText == NULL){
  67.     throw error(1,"Either vertex shader or fragment shader file not found.");
  68.    }
  69.   }catch(error err){
  70.     err.printErr();
  71.     exit(1);
  72.   }
  73.  
  74.   glShaderSource(shader_vp,1,&vsText,0);
  75.   glShaderSource(shader_fp,1,&fsText,0);
  76.  
  77.   glCompileShader(shader_vp);
  78.   validateShader(shader_vp,vsFile);
  79.   glCompileShader(shader_fp);
  80.   validateShader(shader_fp,fsFile);
  81.  
  82.   shader_id = glCreateProgram();
  83.  
  84.   glAttachShader(shader_id,shader_fp);
  85.   glAttachShader(shader_id,shader_vp);
  86.  
  87.   glLinkProgram(shader_id);
  88.   validateProgram(shader_id);
  89. }
  90.  
  91.  
  92. unsigned int Shader::id(){
  93.   return shader_id;
  94. }
  95.  
  96. void Shader::bind(){
  97.   glUseProgram(shader_id);
  98. }
  99.  
  100. void Shader::unbind(){
  101.   glUseProgram(0);
  102. }



Questo č il file shader.vert

Codice sorgente - presumibilmente C/C++

  1. varying vec3 vertex_light_position;
  2. varying vec3 vertex_light_half_vector;
  3. varying vec3 vertex_normal;
  4.  
  5. void main() {            
  6.     vertex_normal = normalize(gl_NormalMatrix * gl_Normal);
  7.    
  8.     vertex_light_position = normalize(gl_LightSource[0].position.xyz);
  9.    
  10.     vertex_light_half_vector = normalize(gl_LightSource[0].halfVector.xyz);
  11.  
  12.     gl_FrontColor = gl_Color;
  13. }



e questo č il file shader.frag

Codice sorgente - presumibilmente C/C++

  1. varying vec3 vertex_light_position;
  2. varying vec3 vertex_light_half_vector;
  3. varying vec3 vertex_normal;
  4.  
  5. void main() {
  6.     vec4 ambient_color = gl_FrontMaterial.ambient * gl_LightSource[0]
  7. .ambient + gl_LightModel.ambient * gl_FrontMaterial.ambient;
  8.  
  9.    
  10.     vec4 diffuse_color = gl_FrontMaterial.diffuse * gl_LightSource[0]
  11. .diffuse;
  12.  
  13.    
  14.     vec4 specular_color = gl_FrontMaterial.specular * gl_LightSource[
  15. 0].specular * pow(max(dot(vertex_normal, vertex_light_half_vector), 0.0) , gl_FrontMaterial.shininess);
  16.  
  17.  
  18.  
  19.     float diffuse_value = max(dot(vertex_normal, vertex_light_position), 0.0);
  20.  
  21.  
  22.     gl_FragColor = ambient_color + diffuse_color * diffuse_value +
  23. specular_color;
  24. }


Mi piacerebbe davvero sapere cosa non va :hail:
Grazie a chi dovesse rispondere


questo č lo screen




EDIT: Okay ce l'ho fatta. Mi sono chiesto cosa cambiasse dalla mia funzione a quella di glut e dopo una ricerca sono giunto ai sorgenti della libreria (per la precisione freeglut_geometry.c) e ho riformulato la funzione in questo modo

Codice sorgente - presumibilmente C/C++

  1. void drawCube(){
  2.  float size = 1;
  3.  
  4.  # define V(a,b,c) glVertex3d(a size,b size,c size);
  5.  # define N(a,b,c) glNormal3d(a,b,c);
  6.     glBegin(GL_QUADS);
  7.         N(1.0,0.0,0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+);
  8.         N(0.0,1.0,0.0); V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+);
  9.         N(0.0,0.0,1.0); V(+,+,+); V(-,+,+); V(-,-,+); V(+,-,+);
  10.         N(-1.0,0.0,0.0); V(-,-,+); V(-,+,+); V(-,+,-); V(-,-,-);
  11.         N(0.0,-1.0,0.0); V(-,-,+); V(-,-,-); V(+,-,-); V(+,-,+);
  12.         N(0.0,0.0,-1.0); V(-,-,-); V(-,+,-); V(+,+,-); V(+,-,-);
  13.     glEnd();
  14.  
  15.  # undef V
  16.  # undef N
  17. }



carlduke ha allegato un file: screen.png (541245 bytes)
Clicca qui per guardare l'immagine

Ultima modifica effettuata da carlduke il 03/02/2011 alle 16:30
PM
Avatar
TheKaneB (Member)
Guru^2


Messaggi: 1792
Iscritto: 26/06/2009

Up
2
Down
V
Segnala al moderatore
Postato alle 5:01
Venerdė, 04/02/2011
che modo orrendo di usare le macro :noway:

gia..pensa che le usa la lib glut..non mi piace soprattutto l'undef a fine funzione - carlduke - 04/02/11 13:23
PM