// This code illustrates basic things that need to be done for texture mapping. /* "class SomeClass" is assumed to be one of your subclasses of SceneElement with instance variables somewhat like the following: GLuint vao[1]; GLuint vbo[3]; GLuint texID; */ /* Recall you will be implementing the various establishXxx methods shown being called below. These are methods in the base SceneElement class. In the context of texture mapping, you will especially want to be sure to implement establishTexture so that it sets all uniform variables as needed. */ SomeClass::SomeClass(const char* texImageSource) : wrapS(GL_CLAMP_TO_BORDER), wrapT(GL_CLAMP_TO_BORDER) { defineInitialGeometry(); texID = readTextureImage(texImageSource); // method in SceneElement } void SomeClass::defineInitialGeometry() { typedef float vec2[2]; typedef float vec3[3]; int numVerticesInYourVBOs = ...; vec3* normals = new vec3[numVerticesInYourVBOs]; vec3* mcPosition = new vec3[numVerticesInYourVBOs]; vec2* texCoords = new vec3[numVerticesInYourVBOs]; // TODO: Fill the normals, mcPosition, and texCoords arrays as desired // NOTE: This assumes a SceneElement subclass that has one VAO that has // positions, normals, and texture coordinates. In general, your // class may have multiple VAOs, only some of which may need texture // mapping. glGenVertexArrays(1, vao); glBindVertexArray(vao[0]); glGenBuffers(3, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glBufferData(GL_ARRAY_BUFFER, numVerticesInYourVBOs*sizeof(vec3), mcPosition, GL_STATIC_DRAW); glVertexAttribPointer(shaderIF->pvaLoc("mcPosition"), 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(shaderIF->pvaLoc("mcPosition")); glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); glBufferData(GL_ARRAY_BUFFER, numVerticesInYourVBOs*sizeof(vec3), normals, GL_STATIC_DRAW); glVertexAttribPointer(shaderIF->pvaLoc("mcNormal"), 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(shaderIF->pvaLoc("mcNormal")); glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); glBufferData(GL_ARRAY_BUFFER, numVerticesInYourVBOs*sizeof(vec2), texCoords, GL_STATIC_DRAW); glVertexAttribPointer(shaderIF->pvaLoc("texCoords"), 2, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(shaderIF->pvaLoc("texCoords")); delete [] normals; delete [] mcPosition; delete [] texCords; } void SomeClass::renderSomeClass() { // NOTE: If your class has multiple VAOs as suggested above in defineInitialGeometry, // what follows would be done for each VAO, and your call to establishTexture // would need to include a parameter that sets the "using a texture map" flag // sent to the fragment shader. establishMaterial(someMaterial); establishTexture(/* ... parameters? ... */); // SEE COMMENTS AT THE TOP OF THIS FILE glBindVertexArray(vao[0]); // ... one or more glDrawArrays and/or glDrawElements calls // TURN OFF your "using a texture map" uniform variable before leaving. } void SomeClass::render() { GLint pgm; glGetIntegerv(GL_CURRENT_PROGRAM, &pgm); glUseProgram(shaderProgram); establishLightingEnvironment(/* ... parameters? ... */); establishView(); renderSomeClass(); glUseProgram(pgm); }