2011-04-08 217 views
1

我需要在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(); 
} 

但廣場是空白。我認爲,位圖是大學提供的一類。如果您需要,我可以提供。

我在做什麼錯?

回答

1

1)您需要在做任何opengl調用之前初始化並創建窗口
2)您致電gluBuild2DMipmaps是錯誤的。目標必須是GLU_TEXTURE_2D
3)結束時,用(後glEnd();)渲染,稱glDisable(GL_TEXTURE_2D);

您還可以檢查每一個OpenGL的調用後使用glGetError OpenGL的錯誤。這應該告訴你哪個呼叫失敗了,爲什麼。檢查失敗呼叫的參考頁面。

+0

他打電話給gluBuild2DMipmaps。 – 2011-04-08 08:03:57

+0

@Stefan是的,我想通了。但他的電話是錯誤的 – 2011-04-08 08:07:17

+0

嗨,謝謝,我改變了函數調用gluBuild2DMipmaps(GLU_TEXTURE_2D,但我越來越「GLU_TEXTURE_2D未定義」...包括(s)我失蹤? – Xenoprimate 2011-04-08 08:14:57

2

您需要在調用OpenGL函數(如glEnable)之前初始化GLUT 之前的函數。否則,它們將無法工作,因爲它們需要OpenGL上下文,而glutCreateWindow是創建這種上下文的方法。

+0

謝謝。我在Display :: Init中調換了兩段代碼,但無濟於事。還有別的嗎? – Xenoprimate 2011-04-08 08:07:07