1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <SDL.h>
#include <GL/glut.h>
void drawScene();
void handleResize(int w,int h);
GLuint TextureID = 0;
SDL_Surface* Surface = SDL_LoadBMP("C:/Documents and Settings/fachenjang/Desktop/Programming/C++/OpenGl/Textures/cb.bmp");
void initRendering() {
glDisable(GL_DEPTH_TEST);
glGetError();
glEnable(GL_NORMALIZE);
glGetError();
glEnable(GL_COLOR_MATERIAL);
glGetError();
glEnable(GL_TEXTURE_2D);
glGetError();
glMatrixMode (GL_PROJECTION);
glGetError();
glLoadIdentity ();
glGetError();
glClearColor(0,0,0,1);
glGetError();
}
void Texture()
{
glGenTextures(1, &TextureID);
glGetError();
glBindTexture(GL_TEXTURE_2D, TextureID);
glGetError();
int Mode = GL_RGB;
if(Surface->format->BytesPerPixel == 4) {
Mode = GL_RGBA;
}
glGetError();
glTexImage2D(GL_TEXTURE_2D, 0, Mode, Surface->w, Surface->h, 0, Mode, GL_UNSIGNED_BYTE, Surface->pixels);
glGetError();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("Textures - SLD & OpenGL");
initRendering();
glutDisplayFunc(drawScene);
//glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMainLoop();
return 0;
}
void handleResize(int w,int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (0, (GLdouble) w, 0, (GLdouble) h);
}
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glGetError();
glMatrixMode(GL_MODELVIEW);
glGetError();
glLoadIdentity();
glGetError();
Texture();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glGetError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGetError();
// For Ortho mode, of course
int X = 0;
int Y = 0;
int Width = 100;
int Height = 100;
glBegin(GL_QUADS);
glColor3f(1,1,1);
glTexCoord2f(0, 0); glVertex3f(X, Y, 0);
glTexCoord2f(1, 0); glVertex3f(X + Width, Y, 0);
glTexCoord2f(1, 1); glVertex3f(X + Width, Y + Height, 0);
glTexCoord2f(0, 1); glVertex3f(X, Y + Height, 0);
glEnd();
glutSwapBuffers();
}
|