C++ OpenGL绘图纹理

C++ OpenGL绘图纹理,c++,opengl,C++,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) {

我需要将纹理映射到三维空间中的正方形

这就是我到目前为止所做的:

#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();
}
但是正方形是白色的。我想位图是大学提供的一门课。如果你需要,我可以提供

我做错了什么?

1)在执行任何opengl调用之前,您需要初始化并创建窗口
2) 你打错电话了。目标必须是
GLU\u纹理\u 2D

3) 完成渲染后(在
glEnd();
之后),调用
glDisable(GL\u TEXTURE\u 2D)


您还可以在每次调用opengl后使用检查opengl错误。这将告诉您哪个呼叫失败以及失败原因。检查失败调用的参考页。

在调用glEnable等OpenGL函数之前,需要初始化GLUT。否则它们将无法工作,因为它们需要一个OpenGL上下文,而glutCreateWindow就是创建这样一个上下文的方法。

他正在调用gluBuild2DMipmaps。您好,谢谢,我将函数调用更改为gluBuild2DMipmaps(GLU_纹理2D,但我得到的是“GLU纹理2D未定义”…我缺少哪些内容?:)编辑:好吧,它在错误的函数中,但是你得到了idea@Motig参考页上说参数必须是GLU_TEXTURE_2D。您可以尝试使用GL_TEXTURE_2DGL_TEXTURE_2D编译,但我仍然得到一个白色正方形。不用了,谢谢。我交换了Display::Init中的两段代码,但没有用。还有别的事吗?