gpt4 book ai didi

c++ - OpenGL 绘图纹理

转载 作者:行者123 更新时间:2023-11-30 03:07:51 25 4
gpt4 key购买 nike

我需要将纹理映射到 3D 空间中的正方形。

这是我目前所拥有的:

#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();
}

但是方 block 是空白的。 Bitmap是大学提供的一个类,我觉得。如果您需要,我可以提供。

我做错了什么?

最佳答案

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

关于c++ - OpenGL 绘图纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5592170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com