// This code example is created for educational purpose // by Thorsten Thormaehlen (contact: www.thormae.de). // It is distributed without any warranty. #include #include // we use glut here as window manager #define _USE_MATH_DEFINES #include #include #include #include #include #include using namespace std; class Renderer { private: struct Vertex { float position[3]; float color[4]; float texCoord[2]; float normal[3]; }; public: float t; private: enum {Pyramid, numVAOs}; enum {PyramidAll, numVBOs}; GLuint vaoID[numVAOs]; GLuint bufID[numVBOs]; int pyramidVertNo; GLuint texID; GLuint progID; GLuint vertID; GLuint fragID; GLint vertexLoc; GLint colorLoc; GLint texCoordLoc; GLint normalLoc; GLint projectionLoc; GLint modelviewLoc; GLint texLoc; float projection[16]; // projection matrix float modelview[16]; // modelview matrix public: // constructor Renderer() : t(0.0), pyramidVertNo(0), texID(0), progID(0), vertID(0), fragID(0), vertexLoc(-1), colorLoc(-1), texCoordLoc(-1), normalLoc(-1), projectionLoc(-1), modelviewLoc(-1), texLoc(-1) {} //destructor ~Renderer() { glDeleteVertexArrays(numVAOs, vaoID); glDeleteBuffers(numVBOs, bufID); glDeleteProgram(progID); glDeleteShader(vertID); glDeleteShader(fragID); } public: void init() { glEnable(GL_DEPTH_TEST); setupShaders(); // create a Vertex Array Objects (VAO) glGenVertexArrays(numVAOs, vaoID); // generate a Vertex Buffer Object (VBO) glGenBuffers(numVBOs, bufID); // bind the pyramid VAO glBindVertexArray(vaoID[Pyramid]); float pyramidVertexData[] = { 0.0f, 0.0f, 2.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.5f, 1.0f, 0.0000f,-0.9701f, 0.2425f, -0.5f,-0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0000f,-0.9701f, 0.2425f, 0.5f,-0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0000f,-0.9701f, 0.2425f, 0.0f, 0.0f, 2.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.5f, 1.0f, 0.9701f, 0.0000f, 0.2425f, 0.5f,-0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.9701f, 0.0000f, 0.2425f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.9701f, 0.0000f, 0.2425f, 0.0f, 0.0f, 2.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f, 1.0f, 0.0000f, 0.9701f, 0.2425f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0000f, 0.9701f, 0.2425f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0000f, 0.9701f, 0.2425f, 0.0f, 0.0f, 2.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.5f, 1.0f,-0.9701f, 0.0000f, 0.2425f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,-0.9701f, 0.0000f, 0.2425f, -0.5f,-0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f,-0.9701f, 0.0000f, 0.2425f }; pyramidVertNo = 12; glBindBuffer(GL_ARRAY_BUFFER, bufID[PyramidAll]); glBufferData(GL_ARRAY_BUFFER, pyramidVertNo*sizeof(Vertex), pyramidVertexData, GL_STATIC_DRAW); int stride = sizeof(Vertex); char *offset = (char*)NULL; // position if(vertexLoc != -1) { glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, GL_FALSE, stride, offset); glEnableVertexAttribArray(vertexLoc); } // color if(colorLoc != -1) { offset = (char*)NULL + 3*sizeof(float); glVertexAttribPointer(colorLoc, 4, GL_FLOAT, GL_FALSE, stride, offset); glEnableVertexAttribArray(colorLoc); } // texCoord if(texCoordLoc != -1) { offset = (char*)NULL + (3+4)*sizeof(float); glVertexAttribPointer(texCoordLoc, 2, GL_FLOAT, GL_FALSE, stride, offset); glEnableVertexAttribArray(texCoordLoc); } // normal if(normalLoc != -1) { offset = (char*)NULL + (3+4+2)*sizeof(float); glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, stride, offset); glEnableVertexAttribArray(normalLoc); } std::string fileName("./checkerboard.ppm"); texID = loadTexture(fileName); } void setupShaders() { // create shader vertID = glCreateShader(GL_VERTEX_SHADER); fragID = glCreateShader(GL_FRAGMENT_SHADER); // load shader source from file std::string vs = loadShaderSrc("./texture.vert"); const char* vss = vs.c_str(); std::string fs = loadShaderSrc("./texture.frag"); const char* fss = fs.c_str(); // specify shader source glShaderSource(vertID, 1, &(vss), NULL); glShaderSource(fragID, 1, &(fss), NULL); // compile the shader glCompileShader(vertID); glCompileShader(fragID); // check for errors printShaderInfoLog(vertID); printShaderInfoLog(fragID); // create program and attach shaders progID = glCreateProgram(); glAttachShader(progID, vertID); glAttachShader(progID, fragID); // "outColor" is a user-provided OUT variable // of the fragment shader. // Its output is bound to the first color buffer // in the framebuffer glBindFragDataLocation(progID, 0, "outputColor"); // link the program glLinkProgram(progID); // output error messages printProgramInfoLog(progID); // retrieve the location of the IN variables of the vertex shader. vertexLoc = glGetAttribLocation(progID,"inputPosition"); colorLoc = glGetAttribLocation(progID, "inputColor"); texCoordLoc = glGetAttribLocation(progID,"inputTexCoord"); normalLoc = glGetAttribLocation(progID, "inputNormal"); // retrieve the location of the UNIFORM variables of the vertex shader. projectionLoc = glGetUniformLocation(progID, "projection"); modelviewLoc = glGetUniformLocation(progID, "modelview"); texLoc = glGetUniformLocation(progID, "myTexture"); } void resize(int w, int h) { glViewport(0, 0, w, h); // this function replaces gluPerspective mat4Perspective(projection, 30.0f, (float)w/(float)h, 1.0f, 10.0f); // mat4Print(projection); } void display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // camera orbits in the z=5 plane // and looks at the origin // mat4LookAt replaces gluLookAt double rad = M_PI / 180.0f * t; mat4LookAt(modelview, 5.0f*float(cos(rad)), 5.0f*float(sin(rad)), 5.0f, // eye 0.0f, 0.0f, 0.5f, // look at 0.0f, 0.0f, 1.0f); // up glUseProgram(progID); // load the current projection and modelview matrix into the // corresponding UNIFORM variables of the shader glUniformMatrix4fv(projectionLoc, 1, false, projection); glUniformMatrix4fv(modelviewLoc, 1, false, modelview); // activate texture unit 0 glActiveTexture(GL_TEXTURE0); // bind texture glBindTexture(GL_TEXTURE_2D, texID); // inform the shader to use texture unit 0 glUniform1i(texLoc, 0); // bind pyramid VAO glBindVertexArray(vaoID[Pyramid]); // render data glDrawArrays(GL_TRIANGLES, 0, pyramidVertNo); } private: void printShaderInfoLog(GLuint obj) { int infoLogLength = 0; int returnLength = 0; char *infoLog; glGetShaderiv(obj, GL_INFO_LOG_LENGTH,&infoLogLength); if (infoLogLength > 0) { infoLog = (char *)malloc(infoLogLength); glGetShaderInfoLog(obj, infoLogLength, &returnLength, infoLog); printf("%s\n",infoLog); free(infoLog); } } void printProgramInfoLog(GLuint obj) { int infoLogLength = 0; int returnLength = 0; char *infoLog; glGetProgramiv(obj, GL_INFO_LOG_LENGTH,&infoLogLength); if (infoLogLength > 0) { infoLog = (char *)malloc(infoLogLength); glGetProgramInfoLog(obj, infoLogLength, &returnLength, infoLog); printf("%s\n",infoLog); free(infoLog); } } std::string loadShaderSrc(const std::string& filename) { std::ifstream is(filename); if (is.is_open()) { std::stringstream buffer; buffer << is.rdbuf(); return buffer.str(); } cerr << "Unable to open file " << filename << endl; exit(1); } // returns a valid textureID on success, otherwise 0 GLuint loadTexture(std::string &filename) { unsigned width; unsigned height; int level = 0; int border = 0; std::vector imgData; // load image data if(!loadPPMImageFlipped(filename, width, height, imgData)) return 0; // data is aligned in byte order glPixelStorei(GL_UNPACK_ALIGNMENT, 1); //request textureID GLuint textureID; glGenTextures( 1, &textureID); // bind texture glBindTexture( GL_TEXTURE_2D, textureID); //define how to filter the texture (important but ignore for now) glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // specify the 2D texture map glTexImage2D(GL_TEXTURE_2D, level, GL_RGB, width, height, border, GL_RGB, GL_UNSIGNED_BYTE, &imgData[0]); // return unique texture identifier return textureID; } bool loadPPMImageFlipped(std::string &filename, unsigned &width, unsigned &height, std::vector &imgData) { ifstream input(filename.c_str(), ifstream::in | ifstream::binary); if(!input) { // cast istream to bool to see if something went wrong cerr << "Can not find texture data file " << filename.c_str() << endl; return false; } input.unsetf(std::ios_base::skipws); string line; input >> line >> std::ws; if (line != "P6") { cerr << "File is not PPM P6 raw format" << endl; return false; } width = 0; height = 0; unsigned depth = 0; unsigned readItems = 0; unsigned char lastCharBeforeBinary; while (readItems < 3) { input >> std::ws; if(input.peek() != '#') { if (readItems == 0) input >> width; if (readItems == 1) input >> height; if (readItems == 2) input >> depth >> lastCharBeforeBinary; readItems++; }else{ // skip comments std::getline(input, line); } } if(depth >= 256) { cerr << "Only 8-bit PPM format is supported" << endl; return false; } unsigned byteCount = width * height * 3; imgData.resize(byteCount); input.read((char*)&imgData[0], byteCount*sizeof(unsigned char)); // vertically flip the image because the image origin // in OpenGL is the lower-left corner unsigned char tmpData; for(unsigned y=0; y < height / 2; y++) { int sourceIndex = y * width * 3; int targetIndex = (height-1-y) * width *3; for(unsigned x=0; x < width*3; x++) { tmpData = imgData[targetIndex]; imgData[targetIndex] = imgData[sourceIndex]; imgData[sourceIndex] = tmpData; sourceIndex++; targetIndex++; } } return true; } // the following functions are some matrix and vector helpers // they work for this demo but in general it is recommended // to use more advanced matrix libaries, // e.g. OpenGL Mathematics (GLM) float vec3Dot( float *a, float *b) { return a[0]*b[0] + a[1]*b[1] + a[2]*b[2]; } void vec3Cross( float *a, float *b, float *res) { res[0] = a[1] * b[2] - b[1] * a[2]; res[1] = a[2] * b[0] - b[2] * a[0]; res[2] = a[0] * b[1] - b[0] * a[1]; } void vec3Normalize(float *a) { float mag = sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]); a[0] /= mag; a[1] /= mag; a[2] /= mag; } void mat4Identity( float *a) { for (int i = 0; i < 16; ++i) a[i] = 0.0f; for (int i = 0; i < 4; ++i) a[i + i * 4] = 1.0f; } void mat4Multiply(float *a, float *b, float *res) { for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { res[j*4 + i] = 0.0f; for (int k = 0; k < 4; ++k) { res[j*4 + i] += a[k*4 + i] * b[j*4 + k]; } } } } void mat4Perspective(float *a, float fov, float aspect, float zNear, float zFar) { float f = 1.0f / float(tan (fov/2.0f * (M_PI / 180.0f))); mat4Identity(a); a[0] = f / aspect; a[1 * 4 + 1] = f; a[2 * 4 + 2] = (zFar + zNear) / (zNear - zFar); a[3 * 4 + 2] = (2.0f * zFar * zNear) / (zNear - zFar); a[2 * 4 + 3] = -1.0f; a[3 * 4 + 3] = 0.0f; } void mat4LookAt(float *viewMatrix, float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ) { float dir[3], right[3], up[3], eye[3]; up[0]=upX; up[1]=upY; up[2]=upZ; eye[0]=eyeX; eye[1]=eyeY; eye[2]=eyeZ; dir[0]=centerX-eyeX; dir[1]=centerY-eyeY; dir[2]=centerZ-eyeZ; vec3Normalize(dir); vec3Cross(dir,up,right); vec3Normalize(right); vec3Cross(right,dir,up); vec3Normalize(up); // first row viewMatrix[0] = right[0]; viewMatrix[4] = right[1]; viewMatrix[8] = right[2]; viewMatrix[12] = -vec3Dot(right, eye); // second row viewMatrix[1] = up[0]; viewMatrix[5] = up[1]; viewMatrix[9] = up[2]; viewMatrix[13] = -vec3Dot(up, eye); // third row viewMatrix[2] = -dir[0]; viewMatrix[6] = -dir[1]; viewMatrix[10] = -dir[2]; viewMatrix[14] = vec3Dot(dir, eye); // forth row viewMatrix[3] = 0.0; viewMatrix[7] = 0.0; viewMatrix[11] = 0.0; viewMatrix[15] = 1.0f; } void mat4Print(float* a) { // opengl uses column major order for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { cout << a[j * 4 + i] << " "; } cout << endl; } } }; //this is a static pointer to a Renderer used in the glut callback functions static Renderer *renderer; //glut static callbacks start static void glutResize(int w, int h) { renderer->resize(w,h); } static void glutDisplay() { renderer->display(); glutSwapBuffers(); glutReportErrors(); } static void timer(int v) { float offset = 1.0f; renderer->t += offset; glutDisplay(); glutTimerFunc(unsigned(20), timer, ++v); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(100,100); glutInitWindowSize(320, 320); glutCreateWindow("Accessing textures in the shader"); GLenum err = glewInit(); if (GLEW_OK != err) { fprintf(stderr, "Glew error: %s\n", glewGetErrorString(err)); } glutDisplayFunc(glutDisplay); //glutIdleFunc(glutDisplay); glutReshapeFunc(glutResize); renderer = new Renderer; renderer->init(); glutTimerFunc(unsigned(20), timer, 0); glutMainLoop(); }