我需要在3D空間中將紋理映射到正方形。OpenGL繪圖紋理
這是我到目前爲止有:
#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_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();
}
但廣場是空白。我認爲,位圖是大學提供的一類。如果您需要,我可以提供。
我在做什麼錯?
他打電話給gluBuild2DMipmaps。 – 2011-04-08 08:03:57
@Stefan是的,我想通了。但他的電話是錯誤的 – 2011-04-08 08:07:17
嗨,謝謝,我改變了函數調用gluBuild2DMipmaps(GLU_TEXTURE_2D,但我越來越「GLU_TEXTURE_2D未定義」...包括(s)我失蹤? – Xenoprimate 2011-04-08 08:14:57