void Shader::initialize() { light.Position = glm::vec3(0, 0, 2); light.Color = glm::vec3(1, 1, 1); glUseProgram(program); positionHandle = safe_glGetAttribLocation(program, "position"); normalHandle = safe_glGetAttribLocation(program, "normal"); projMatrixHandle = safe_glGetUniformLocation(program, "projMatrix"); viewMatrixHandle = safe_glGetUniformLocation(program, "viewMatrix"); modelMatrixHandle = safe_glGetUniformLocation(program, "modelMatrix"); materialHandle.Diffuse = safe_glGetUniformLocation(program, "material.diffuse"); materialHandle.Specular = safe_glGetUniformLocation(program, "material.specular"); materialHandle.Ambient = safe_glGetUniformLocation(program, "material.ambient"); materialHandle.Shininess = safe_glGetUniformLocation(program, "material.shininess"); lightHandle.Position = safe_glGetUniformLocation(program, "light.position"); lightHandle.Color = safe_glGetUniformLocation(program, "light.color"); printf("sucessfully installed shader %d\n", program); }
/*function to help load the shader */ int InstallShader(const GLchar *vShaderName) { GLuint VS; //handles to shader object GLint vCompiled, linked; //status of shader VS = glCreateShader(GL_VERTEX_SHADER); //load the source glShaderSource(VS, 1, &vShaderName, NULL); //compile shader and print log glCompileShader(VS); /* check shader status requires helper functions */ printOpenGLError(); glGetShaderiv(VS, GL_COMPILE_STATUS, &vCompiled); printShaderInfoLog(VS); if (!vCompiled) { printf("Error compiling the shader %s", vShaderName); return 0; } //create a program object and attach the compiled shader ShadeProg = glCreateProgram(); glAttachShader(ShadeProg, VS); glLinkProgram(ShadeProg); /* check shader status requires helper functions */ printOpenGLError(); glGetProgramiv(ShadeProg, GL_LINK_STATUS, &linked); printProgramInfoLog(ShadeProg); glUseProgram(ShadeProg); /* get handles to attribute data */ h_aPosition = safe_glGetAttribLocation(ShadeProg, "aPosition"); h_aColor = safe_glGetAttribLocation(ShadeProg, "aColor"); printf("sucessfully installed shader %d\n", ShadeProg); return 1; }
/* for this assignment we are doing anything interesting with the fragment sahder */ int InstallShader(const GLchar *vShaderName, const GLchar *fShaderName) { GLuint VS; //handles to shader object GLuint FS; //handles to frag shader object GLint vCompiled, fCompiled, linked; //status of shader printf("IS1\n"); VS = glCreateShader(GL_VERTEX_SHADER); FS = glCreateShader(GL_FRAGMENT_SHADER); printf("IS3\n"); //load the source glShaderSource(VS, 1, &vShaderName, NULL); glShaderSource(FS, 1, &fShaderName, NULL); printf("IS2\n"); //compile shader and print log glCompileShader(VS); /* check shader status requires helper functions */ glGetShaderiv(VS, GL_COMPILE_STATUS, &vCompiled); //compile shader and print log glCompileShader(FS); /* check shader status requires helper functions */ glGetShaderiv(FS, GL_COMPILE_STATUS, &fCompiled); if (!vCompiled || !fCompiled) { printf("Error compiling either shader %s or %s", vShaderName, fShaderName); return 0; } //create a program object and attach the compiled shader ShadeProg = glCreateProgram(); glAttachShader(ShadeProg, VS); glAttachShader(ShadeProg, FS); glLinkProgram(ShadeProg); /* check shader status requires helper functions */ glGetProgramiv(ShadeProg, GL_LINK_STATUS, &linked); glUseProgram(ShadeProg); printf("IS10\n"); /* get handles to attribute and uniform data in shader */ /* get handles to attribute data */ h_aPosition = safe_glGetAttribLocation(ShadeProg, "aPosition"); h_aNormal = safe_glGetAttribLocation(ShadeProg, "aNormal"); h_uProjMatrix = safe_glGetUniformLocation(ShadeProg, "uProjMatrix"); h_uViewMatrix = safe_glGetUniformLocation(ShadeProg, "uViewMatrix"); h_uModelMatrix = safe_glGetUniformLocation(ShadeProg, "uModelMatrix1"); h_uLightVec = safe_glGetUniformLocation(ShadeProg, "aLightVec"); h_uLightColor = safe_glGetUniformLocation(ShadeProg, "uLColor"); h_uMatAmb = safe_glGetUniformLocation(ShadeProg, "uMat.aColor"); h_uMatDif = safe_glGetUniformLocation(ShadeProg, "uMat.dColor"); h_uMatSpec = safe_glGetUniformLocation(ShadeProg, "uMat.sColor"); h_uMatShine = safe_glGetUniformLocation(ShadeProg, "uMat.shine"); h_uCamPos = safe_glGetUniformLocation(ShadeProg, "uCamPos"); h_uShadeMode = safe_glGetUniformLocation(ShadeProg, "uShadeMode"); printf("sucessfully installed shader %d\n", ShadeProg); return 1; }
/*function to help load the shader - note current version only loading a vertex shader */ int InstallShader(const GLchar *vShaderName, const GLchar *fShaderName) { GLuint VS, FS; //handles to shader Model GLint vCompiled, fCompiled, linked; //status of shader VS = glCreateShader(GL_VERTEX_SHADER); FS = glCreateShader(GL_FRAGMENT_SHADER); //load the source glShaderSource(VS, 1, &vShaderName, NULL); glShaderSource(FS, 1, &fShaderName, NULL); //compile shader and print log glCompileShader(VS); /* check shader status requires helper functions */ printOpenGLError(); glGetShaderiv(VS, GL_COMPILE_STATUS, &vCompiled); printShaderInfoLog(VS); //compile shader and print log glCompileShader(FS); /* check shader status requires helper functions */ printOpenGLError(); glGetShaderiv(FS, GL_COMPILE_STATUS, &fCompiled); printShaderInfoLog(FS); if (!vCompiled || !fCompiled) { printf("Error compiling the shader %s", vShaderName); return 0; } //create a program Model and attach the compiled shader ShadeProg = glCreateProgram(); glAttachShader(ShadeProg, VS); glAttachShader(ShadeProg, FS); glLinkProgram(ShadeProg); /* check shader status requires helper functions */ printOpenGLError(); glGetProgramiv(ShadeProg, GL_LINK_STATUS, &linked); printProgramInfoLog(ShadeProg); glUseProgram(ShadeProg); /* get handles to attribute data */ h_aPosition = safe_glGetAttribLocation(ShadeProg, "aPosition"); h_aNormal = safe_glGetAttribLocation(ShadeProg, "aNormal"); h_uProjMatrix = safe_glGetUniformLocation(ShadeProg, "uProjMatrix"); h_uViewMatrix = safe_glGetUniformLocation(ShadeProg, "uViewMatrix"); h_uModelMatrix = safe_glGetUniformLocation(ShadeProg, "uModelMatrix"); h_uShadeType = safe_glGetUniformLocation(ShadeProg, "uShadeType"); h_uCamPos = safe_glGetUniformLocation(ShadeProg, "uCamPos"); h_uSun = safe_glGetUniformLocation(ShadeProg, "uSun"); h_uLightColor = safe_glGetUniformLocation(ShadeProg, "uLColor"); h_uMatAmb = safe_glGetUniformLocation(ShadeProg, "uMat.aColor"); h_uMatDif = safe_glGetUniformLocation(ShadeProg, "uMat.dColor"); h_uMatSpec = safe_glGetUniformLocation(ShadeProg, "uMat.sColor"); h_uMatShine = safe_glGetUniformLocation(ShadeProg, "uMat.shine"); h_aTexCoord = safe_glGetAttribLocation(ShadeProg, "aTexCoord"); h_uTexUnit = safe_glGetUniformLocation(ShadeProg, "uTexUnit"); printf("sucessfully installed shader %d\n", ShadeProg); return 1; }