Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include <cstdio>
- #include <string>
- #include <iostream>
- #if defined(__APPLE__) || defined(MACOSX)
- #include <OpenGL/gl.h>
- #include <GLUT/glut.h>
- #else
- #include <GL/gl.h>
- #include <GL/glut.h>
- #endif
- GLuint textureId;
- #define glCheck(call) ((call), checkError(__FILE__, __LINE__))
- void checkError(const std::string& file, unsigned int line)
- {
- // Get the last error
- GLenum errorCode = glGetError();
- if (errorCode != GL_NO_ERROR)
- {
- std::string error = "unknown error";
- std::string description = "no description";
- // Decode the error code
- switch (errorCode)
- {
- case GL_INVALID_ENUM :
- {
- error = "GL_INVALID_ENUM";
- description = "an unacceptable value has been specified for an enumerated argument";
- break;
- }
- case GL_INVALID_VALUE :
- {
- error = "GL_INVALID_VALUE";
- description = "a numeric argument is out of range";
- break;
- }
- case GL_INVALID_OPERATION :
- {
- error = "GL_INVALID_OPERATION";
- description = "the specified operation is not allowed in the current state";
- break;
- }
- case GL_STACK_OVERFLOW :
- {
- error = "GL_STACK_OVERFLOW";
- description = "this command would cause a stack overflow";
- break;
- }
- case GL_STACK_UNDERFLOW :
- {
- error = "GL_STACK_UNDERFLOW";
- description = "this command would cause a stack underflow";
- break;
- }
- case GL_OUT_OF_MEMORY :
- {
- error = "GL_OUT_OF_MEMORY";
- description = "there is not enough memory left to execute the command";
- break;
- }
- case GL_INVALID_FRAMEBUFFER_OPERATION_EXT :
- {
- error = "GL_INVALID_FRAMEBUFFER_OPERATION_EXT";
- description = "the object bound to FRAMEBUFFER_BINDING_EXT is not \"framebuffer complete\"";
- break;
- }
- }
- // Log the error
- std::cout << "An internal OpenGL call failed in "
- << file.substr(file.find_last_of("\\/") + 1) << " (" << line << ") : "
- << error << ", " << description
- << std::endl;
- }
- }
- void render(void)
- {
- glCheck(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
- glCheck(glLoadIdentity());
- glCheck(glBindTexture(GL_TEXTURE_2D, textureId));
- glCheck(glRotatef(1, 0, 0, 5));
- glCheck(glPushMatrix());
- glCheck(glTranslatef(0.1, 0.1, 0));
- glCheck(glScalef(0.8, 0.8, 0.8));
- glCheck(glBegin(GL_QUADS));
- {
- glCheck(glTexCoord2f(0.0f, 0.0f));
- glCheck(glVertex2f(-1.0f, -1.0f)); // Bottom Left Of The Texture and Quad
- glCheck(glTexCoord2f(1.0f, 0.0f));
- glCheck(glVertex2f( 1.0f, -1.0f)); // Bottom Right Of The Texture and Quad
- glCheck(glTexCoord2f(1.0f, 1.0f));
- glCheck(glVertex2f( 1.0f, 1.0f)); // Top Right Of The Texture and Quad
- glCheck(glTexCoord2f(0.0f, 1.0f));
- glCheck(glVertex2f(-1.0f, 1.0f)); // Top Left Of The Texture and Quad
- }
- glCheck(glEnd());
- glCheck(glPopMatrix());
- glutSwapBuffers();
- }
- void setup()
- {
- glCheck(glClearColor(1, 0, 0, 1));
- unsigned width = 128;
- unsigned heigth = 128;
- unsigned char *data = new unsigned char[width * heigth * 4];
- assert(data != NULL);
- for (unsigned x = 0; x < width;x++)
- {
- for (unsigned y = 0; y < heigth;y++)
- {
- if ((y * width + x) % 2 == 0)
- {
- data[(x * heigth + y) * 4 + 0] = 0;
- data[(x * heigth + y) * 4 + 1] = 0;
- data[(x * heigth + y) * 4 + 2] = 255;
- data[(x * heigth + y) * 4 + 3] = 255;
- }
- else
- {
- data[(x * heigth + y) * 4 + 0] = 255;
- data[(x * heigth + y) * 4 + 1] = 0;
- data[(x * heigth + y) * 4 + 2] = 0;
- data[(x * heigth + y) * 4 + 3] = 255;
- }
- }
- }
- glCheck(glEnable(GL_TEXTURE_2D));
- glCheck(glGenTextures(1, &textureId));
- glCheck(glBindTexture(GL_TEXTURE_2D, textureId));
- glCheck(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, heigth, 0, GL_RGBA, GL_UNSIGNED_BYTE, data));
- glCheck(glMatrixMode(GL_MODELVIEW));
- glCheck(glLoadIdentity());
- }
- int main( int argc, char** argv )
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
- glutInitWindowPosition( 20, 60 );
- glutInitWindowSize( 360, 360 );
- glutCreateWindow( "Texture test" );
- setup();
- glutDisplayFunc(render);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement