carlduke (Member)
Pro
Messaggi: 153
Iscritto: 29/01/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++ |
#include "Shader.h" #include <stdlib.h> Shader shader; float angle = 0.0; GLfloat lx = 0.0; GLfloat ly = 1.0; GLfloat lz = 1.0; GLfloat lw = 0.0; void init(void) { glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); shader.init("shader.vert", "shader.frag"); } void drawCube(){ glBegin(GL_QUADS); glVertex3f(1.0,-1.0,-1.0); glVertex3f(1.0,-1.0,1.0); glVertex3f(-1.0,-1.0,1.0); glVertex3f(-1.0,-1.0,-1.0); glVertex3f(1.0,1.0,-1.0); glVertex3f(1.0,1.0,1.0); glVertex3f(-1.0,1.0,1.0); glVertex3f(-1.0,1.0,-1.0); glVertex3f(-1.0,-1.0,-1.0); glVertex3f(-1.0,1.0,-1.0); glVertex3f(1.0,1.0,-1.0); glVertex3f(1.0,-1.0,-1.0); glVertex3f(-1.0,-1.0,1.0); glVertex3f(-1.0,1.0,1.0); glVertex3f(1.0,1.0,1.0); glVertex3f(1.0,-1.0,1.0); glVertex3f(1.0,1.0,1.0); glVertex3f(1.0,-1.0,1.0); glVertex3f(1.0,-1.0,-1.0); glVertex3f(1.0,1.0,-1.0); glVertex3f(-1.0,1.0,1.0); glVertex3f(-1.0,-1.0,1.0); glVertex3f(-1.0,-1.0,-1.0); glVertex3f(-1.0,1.0,-1.0); glEnd(); } void cube(){ glRotatef(angle, 1.0, 0.0, 0.0); //rotate on the x axis glRotatef(angle, 0.0, 1.0, 0.0); //rotate on the y axis glRotatef(angle, 0.0, 0.0, 1.0); //rotate on the z axis GLfloat mShininess[] = {50}; GLfloat DiffuseMaterial[] = {1.0, 0.0, 0.0}; GLfloat AmbientMaterial[] = {0.0, 0.0, 0.0}; GLfloat SpecularMaterial[] = {1.0, 1.0, 1.0}; glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, DiffuseMaterial); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, AmbientMaterial); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, SpecularMaterial); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mShininess); drawCube(); //glutSolidCube(2.0); //glutSolidTeapot(2.0); } void setLighting(void) { GLfloat DiffuseLight[] = {1.0, 1.0, 1.0}; GLfloat AmbientLight[] = {0.2, 0.2, 0.2}; GLfloat SpecularLight[] = {1.0, 1.0, 1.0}; glLightfv (GL_LIGHT0, GL_SPECULAR, SpecularLight); glLightfv (GL_LIGHT0, GL_DIFFUSE, DiffuseLight); glLightfv (GL_LIGHT0, GL_AMBIENT, AmbientLight); GLfloat LightPosition[] = {lx, ly, lz, lw}; //set the LightPosition to the specified values glLightfv (GL_LIGHT0, GL_POSITION, LightPosition); //change the light accordingly } void display (void) { glClearColor (0.0,0.0,0.0,1.0); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); setLighting(); shader.bind(); cube(); shader.unbind(); glutSwapBuffers(); angle += 0.02f; } void reshape (int w, int h) { glViewport (0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0); glMatrixMode (GL_MODELVIEW); } int main (int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); //set up the double buffering glutInitWindowSize(800, 600); glutInitWindowPosition(100, 100); glutCreateWindow("A basic OpenGL Window"); glewInit(); glutDisplayFunc(display); glutIdleFunc(display); glutReshapeFunc(reshape); init(); glutMainLoop(); return 0; }
|
Questo č il file Shader.h
Codice sorgente - presumibilmente C++ |
#ifndef SHADER_H #define SHADER_H #include <stdlib.h> #include <GL/glew.h> #include <GL/glut.h> #include <string> #include "string_extra.h" class Shader{ public : Shader(); Shader(const char* vsFile,const char* fsFile); ~Shader(); void init(const char* vsFile,const char* fsFile); void bind(); void unbind(); unsigned int id(); protected : unsigned int shader_id; unsigned int shader_vp; unsigned int shader_fp; }; #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++ |
#include "Shader.h" #include <string> #include <stdlib.h> #include "string_extra.h" #include "Error.h" static void validateShader(GLuint shader,const char* file = 0){ const unsigned int BUFFER_SIZE = 512; char buffer[BUFFER_SIZE]; memset(buffer,0,BUFFER_SIZE); GLsizei length = 0; glGetShaderInfoLog(shader,BUFFER_SIZE,&length,buffer); if(length > 0){ cerr<<"Shader "<<shader<<" ("<<(file?file:" ")<<") compile error: "<<buffer<<endl; } } static void validateProgram(GLuint program){ const unsigned int BUFFER_SIZE = 512; char buffer[BUFFER_SIZE]; memset(buffer,0,BUFFER_SIZE); GLsizei length = 0; memset(buffer,0,BUFFER_SIZE); glGetProgramInfoLog(program, BUFFER_SIZE, &length, buffer); if (length > 0) cerr<<"Program "<<program<<" link error: "<<buffer<< endl; glValidateProgram(program); GLint status; glGetProgramiv(program,GL_VALIDATE_STATUS,&status); if (status == GL_FALSE) cerr<<"Error validating shader "<<program<<endl; } Shader::Shader(){ } Shader::Shader(const char* vsFile,const char* fsFile){ init(vsFile,fsFile); } Shader::~Shader(){ glDetachShader(shader_id,shader_fp); glDetachShader(shader_id,shader_vp); glDeleteShader(shader_fp); glDeleteShader(shader_vp); glDeleteProgram(shader_id); } void Shader::init(const char* vsFile,const char* fsFile){ shader_vp = glCreateShader(GL_VERTEX_SHADER); shader_fp = glCreateShader(GL_FRAGMENT_SHADER); const char* vsText = textFileRead(vsFile); const char* fsText = textFileRead(fsFile); try{ if(vsText == NULL || fsText == NULL){ throw error(1,"Either vertex shader or fragment shader file not found."); } }catch(error err){ err.printErr(); exit(1); } glShaderSource(shader_vp,1,&vsText,0); glShaderSource(shader_fp,1,&fsText,0); glCompileShader(shader_vp); validateShader(shader_vp,vsFile); glCompileShader(shader_fp); validateShader(shader_fp,fsFile); shader_id = glCreateProgram(); glAttachShader(shader_id,shader_fp); glAttachShader(shader_id,shader_vp); glLinkProgram(shader_id); validateProgram(shader_id); } unsigned int Shader::id(){ return shader_id; } void Shader::bind(){ glUseProgram(shader_id); } void Shader::unbind(){ glUseProgram(0); }
|
Questo č il file shader.vert
Codice sorgente - presumibilmente C/C++ |
varying vec3 vertex_light_position;
varying vec3 vertex_light_half_vector;
varying vec3 vertex_normal;
void main() {
vertex_normal = normalize(gl_NormalMatrix * gl_Normal);
vertex_light_position = normalize(gl_LightSource[0].position.xyz);
vertex_light_half_vector = normalize(gl_LightSource[0].halfVector.xyz);
gl_FrontColor = gl_Color;
}
|
e questo č il file shader.frag
Codice sorgente - presumibilmente C/C++ |
varying vec3 vertex_light_position;
varying vec3 vertex_light_half_vector;
varying vec3 vertex_normal;
void main() {
vec4 ambient_color = gl_FrontMaterial.ambient * gl_LightSource[0]
.ambient + gl_LightModel.ambient * gl_FrontMaterial.ambient;
vec4 diffuse_color = gl_FrontMaterial.diffuse * gl_LightSource[0]
.diffuse;
vec4 specular_color = gl_FrontMaterial.specular * gl_LightSource[
0].specular * pow(max(dot(vertex_normal, vertex_light_half_vector), 0.0) , gl_FrontMaterial.shininess);
float diffuse_value = max(dot(vertex_normal, vertex_light_position), 0.0);
gl_FragColor = ambient_color + diffuse_color * diffuse_value +
specular_color;
}
|
Mi piacerebbe davvero sapere cosa non va
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++ |
void drawCube(){
float size = 1;
# define V(a,b,c) glVertex3d(a size,b size,c size);
# define N(a,b,c) glNormal3d(a,b,c);
glBegin(GL_QUADS);
N(1.0,0.0,0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+);
N(0.0,1.0,0.0); V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+);
N(0.0,0.0,1.0); V(+,+,+); V(-,+,+); V(-,-,+); V(+,-,+);
N(-1.0,0.0,0.0); V(-,-,+); V(-,+,+); V(-,+,-); V(-,-,-);
N(0.0,-1.0,0.0); V(-,-,+); V(-,-,-); V(+,-,-); V(+,-,+);
N(0.0,0.0,-1.0); V(-,-,-); V(-,+,-); V(+,+,-); V(+,-,-);
glEnd();
# undef V
# undef N
}
|
Ultima modifica effettuata da carlduke il 03/02/2011 alle 16:30 |