OpenGL Drawing Textures
I need to map a texture to a square in 3D space.
This is what I have so far:
#include "Display.h"
#include "Bitmap.h"
#include "Glut/glut.h"
#include "GL/gl.h"
#include "GL/glu.h"
Bitmap Display::m_HeightMap;
unsigned int Display::m_TextureID;
void Display::Init(int argc, char ** argv)
{
glEnable(GL_TEXTURE_2D);
Bitmap image;
image.loadBMP("myTexture.bmp");
glGenTextures(1, &m_TextureID); //Generates 1 texture, puts the ID in m_TextureID
glBindTexture(GL_TEXTURE_2D, m_TextureID); //Sets m_TextureID as the current textureID
//All below are just functions to set up the rendering modes of the texture
glTexEnvf(GL_TEXTURE_ENV, GL_开发者_JAVA百科TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE, image.data); //Copy image to graphics card
glutInit(&argc, argv); // initializes glut
// sets display mode. These parameter set RGB colour model
// and double buffering.
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("OpenGL Tutorial");
// Set glut callback functions
glutDisplayFunc(Display::DisplayScene);
glutIdleFunc(Display::Idle);
glutReshapeFunc(Display::Resize);
// Begin glut main loop
glutMainLoop();
}
void Display::DisplayScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the back buffer
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_TextureID);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-2, 2, -3);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(2, 2, -3);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(2, -2, -3);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-2, -2, -3);
glEnd();
glutSwapBuffers(); // Swap the front and back buffers
}
void Display::Resize(int w, int h)
{
/* Resize is called when window is resized */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,w,h);
gluPerspective(45, (float)w/(float)h, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,10,
0,0,0,
0,1,0);
}
void Display::Idle()
{
/* When nothing else is happening, idle is called.
* Simulation should be done here and then
* the display method should be called
*/
glutPostRedisplay();
}
But the square is blank white. Bitmap is a class provided by the university, I think. I can provide it if you need.
What am I doing wrong?
You need to initialize GLUT before calling OpenGL functions like glEnable. Otherwise they won't work, because they require an OpenGL context to be present, and glutCreateWindow is the method that creates such a context.
1) you need to initialize and create window before doing any opengl call
2) Your call to gluBuild2DMipmaps is wrong. The target has to be GLU_TEXTURE_2D
3) when finish with rendering (after glEnd();
), call glDisable(GL_TEXTURE_2D);
You can also check for opengl error using glGetError after every opengl call. That should tell you which call failed and why. Check the reference pages of the failed call.
精彩评论