Skip to main content
gamedev.net
🔒 Locked

glBindTexture Problem!!!

Started by Kevinito Sep 1, 2006 at 7:01 AM 3 replies 2.3k views
Original Post
Kevinito
Kevinito
Greetings, I am currently coding a screensaver using MSVC++. I am loading a 256x256 bitmap graphic and using glBindTexture with glTexCoord2f to apply the texture on the cube. The screensaver compiles and executes perfectly, but without the texture applied to the cube!!! Here is the code for the OpenGL screensaver. I left out the screensaver code . . . Thanks, Kevinito // All graphical functions can go in this file #include #include #include #include #include // Header for OpenGL32 Lib #include // GLu32 Lib #include // GLaux Lib // #include "resource.h" // Include these libraries #pragma comment(lib, "OpenGL32.lib") #pragma comment(lib, "GLu32.lib") #pragma comment(lib, "GLaux.lib") #pragma comment(lib, "ScrnSave.lib") #pragma comment(lib, "comctl32.lib") // Screen saver library contains the main function and // other startup code required for a scrnsaver // setup for OpenGL // HDC hDC=NULL; // HGLRC hRC=NULL; // HWND hWnd=NULL; // HINSTANCE hInstance; // bool keys[256]; // bool active=TRUE; // bool fullscreen=TRUE; GLuint texture[1]; // LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); AUX_RGBImageRec *LoadBMP(char *Filename) { FILE *File=NULL; if (!Filename) { return NULL; } File=fopen(Filename,"r"); if (File) { fclose(File); return auxDIBImageLoad(Filename); } return NULL; } int LoadGLTextures() { int Status=FALSE; AUX_RGBImageRec *TextureImage[1]; memset(TextureImage,0,sizeof(void *)*1); if (TextureImage[0]=LoadBMP("Data/image1.bmp")) { Status=TRUE; glGenTextures(1, &texture[0]); glBindTexture(GL_TEXTURE_2D, texture[0]); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } if (TextureImage[0]) { if (TextureImage[0]->data) { free(TextureImage[0]->data); } free(TextureImage[0]); } return Status; } void SetupAnimation(int Width, int Height) { // Current viewport:x, y, width, height glViewport(0, 0, (GLsizei)Width, (GLsizei)Height); // Next we set up for a perspective view glMatrixMode(GL_PROJECTION); // Select the Projection Matrix glLoadIdentity(); // Reset(init) the Projection Matrix gluPerspective(45.0f, (GLfloat)Width/(GLfloat)Height, 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); // Select the modelview matrix glLoadIdentity(); // Reset (init) the modelview matrix gluLookAt(0.0f, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glEnable(GL_TEXTURE_2D); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Set background to black glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glShadeModel(GL_SMOOTH); // Enable smooth shading glClearColor(0.0f, 0.0f, 0.0f, 0.5f); glClearDepth(1.0f); // Clear depth buffer glEnable(GL_DEPTH_TEST); // Enables depth test glDepthFunc(GL_LEQUAL); // Type of depth test to perform // Enhances image quality glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // glColor3f(0.5f, 0.5f, 1.0f); } // Declare rotation variables static GLfloat spinx, spiny, spinz; // OpenGL drawing and animation void OnTimer(HDC hDC) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Rotation variables spinx = 0.1f; spiny = -0.2f; spinz = 0.3f; // glTranslatef(-1.5f,0.0f,-6.0f); // Move Left And Into The Screen glRotatef(spinx, 1.0f, 0.0f, 0.0f); glRotatef(spiny, 0.0f, 1.0f, 0.0f); glRotatef(spinz, 0.0f, 0.0f, 1.0f); glBindTexture(GL_TEXTURE_2D, texture[0]); glBegin(GL_QUADS); // Front Face glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left // Back Face glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Face glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Bottom Face glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Right face glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Left Face glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glEnd(); SwapBuffers(hDC); }
Eitsch
Eitsch
use glGetError

this topic should be in the openglforum
Dybbuk
Dybbuk
Are you perhaps loading your textures before you create your openGL context.

You have to create the opengl context first, otherwise textures won't show up.

(Textures are bound to the current, so if you didn't call WGLmakecurrent (or somthing like that) OpenGL doesn't know to wich context the textures belong.
Mushu
Mushu
Make sure you call glEnable( GL_TEXTURE_2D ) before creating the texture object, otherwise it won't work.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.