因此,我使用 GLFW 进行窗口管理,并使用 GLEW 进行扩展管理。我正在尝试编写我兄弟提出的跨平台游戏。这是一个纸牌游戏。但是,为了获得在 3D 表面上纹理映射和渲染的基础知识,我试图让一些卡片纹理显示在立方体的侧面。我的第一个问题是 glfw 的 TGA 加载器,其中只有要加载的第一个纹理是可显示的(尽管它会在我尝试纹理的每个表面上正确显示),尽管已为 glGenTextures 使用了 GLuint 指针数组。然后,我找到了http://home.comcast.net/~mlyn7576/OpenGL-TGA/,它似乎也可以工作,但现在只显示最后加载的纹理,并且它在所有面上显示一个纹理,即使我只引用它的 GL 名称一次。那么我做错了什么?我不明白为什么只显示一个加载的纹理。
这是我的主要标题:
#ifndef _RPSEVOLVED_H_
#define _RPSEVOLVED_H_
#endif
#include <iostream>
#include <math.h>
#include <gl\glew.h>
#include <gl\glfw.h>
#include <stdint.h>
using namespace std;
class RPSEvolved
{
private:
bool Running;
public:
static RPSEvolved* instance;
RPSEvolved();
int OnExecute();
GLuint cardBackId;
GLuint bgId;
GLuint airCardId;
GLuint earthCardId;
GLuint fireCardId;
GLuint lightningCardId;
GLuint waterCardId;
public:
bool OnInit();
void OnEvent(/*SDL_Event* ev*/);
void OnLoop();
void OnRender();
void OnCleanup();
//callbacks
static void GLFWCALL OnKeyCB(int key, int action);
static void OnWindowCloseCB();
static RPSEvolved* GetInstance();
void GLFWCALL OnCharCB(int key, int action);
void GLFWCALL OnMouseWheelCB(int pos);
void GLFWCALL OnMouseMoveCB(int x, int y);
void GLFWCALL OnMouseButtonCB(int button, int action);
};
这是我的初始化代码:
#include "RPSEvolved.h"
#include "RPSE_TGA.h"
bool RPSEvolved::OnInit()
{
if(glfwInit() == GL_FALSE)
{
cout << "Error initializing GLFW";
return false;
}
if(glfwOpenWindow(1024, 768, 8, 8, 8, 8, 32, 32, GLFW_WINDOW) == GL_FALSE)
{
cout << "Error initializing GLFW";
return false;
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_TEXTURE_2D);
glEnable(GL_NORMALIZE); //Automatically normalize normals
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
gluPerspective(45, 1024/768, 1, 100);
glMatrixMode(GL_MODELVIEW);
glfwDisable(GLFW_AUTO_POLL_EVENTS);
glClearColor(0, 0, 0, 255);
glfwSetWindowCloseCallback((GLFWwindowclosefun)OnWindowCloseCB);
glfwSetKeyCallback(OnKeyCB);
cardBackId = load_texture_TGA("images\\cardBack.tga", NULL, NULL, GL_CLAMP, GL_CLAMP);
bgId = load_texture_TGA("images\\bg.tga", NULL, NULL, GL_CLAMP, GL_CLAMP);
airCardId = load_texture_TGA("images\\airCard.tga", NULL, NULL, GL_CLAMP, GL_CLAMP);
earthCardId = load_texture_TGA("images\\earthCard.tga", NULL, NULL, GL_CLAMP, GL_CLAMP);
fireCardId = load_texture_TGA("images\\fireCard.tga", NULL, NULL, GL_CLAMP, GL_CLAMP);
lightningCardId = load_texture_TGA("images\\lightningCard.tga", NULL, NULL, GL_CLAMP, GL_CLAMP);
waterCardId = load_texture_TGA("images\\waterCard.tga", NULL, NULL, GL_CLAMP, GL_CLAMP);
return true;
}
我的显示代码:
#include "RPSEvolved.h"
void RPSEvolved::OnRender()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gluLookAt(0.0f, 5.0f, 30.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
static float _angle = 0.0f; _angle -= 0.3f;
glEnable(GL_TEXTURE_2D);
glRotatef(_angle, 0.0f, 1.0f, 0.0f);
glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
glBindTexture(GL_TEXTURE_2D, cardBackId);
glNormal3f(0.0f, 0.0f, 1.0f); // front
glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.0f, -5.0f, 3.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(3.0f, -5.0f, 3.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(3.0f, 5.0f, 3.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-3.0f, 5.0f, 3.0f);
glBindTexture(GL_TEXTURE_2D, airCardId);
glNormal3f(1.0f, 0.0f, 0.0f); // right
glTexCoord2f(0.0f, 0.0f); glVertex3f(3.0f, -4.0f, 3.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(3.0f, -4.0f, -3.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(3.0f, 4.0f, -3.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(3.0f, 4.0f, 3.0f);
glBindTexture(GL_TEXTURE_2D, earthCardId);
glNormal3f(0.0f, 0.0f, -1.0f); // back
glTexCoord2f(0.0f, 0.0f); glVertex3f(3.0f, 4.0f, -3.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-3.0f, 4.0f, -3.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-3.0f, -4.0f, -3.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(3.0f, -4.0f, -3.0f);
glBindTexture(GL_TEXTURE_2D, fireCardId);
glNormal3f(-1.0f, 0.0f, 0.0f); // left
glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.0f, 4.0f, -3.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-3.0f, 4.0f, 3.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-3.0f, -4.0f, 3.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-3.0f, -4.0f, -3.0f);
glBindTexture(GL_TEXTURE_2D, lightningCardId);
glNormal3f(0.0f, 1.0f, 0.0f); // top
glTexCoord2f(0.0f, 0.0f); glVertex3f(3.0f, 4.0f, 3.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-3.0f, 4.0f, 3.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-3.0f, 4.0f, -3.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(3.0f, 4.0f, -3.0f);
glBindTexture(GL_TEXTURE_2D, waterCardId);
glNormal3f(0.0f, -1.0f, 0.0f); // top
glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.0f, -4.0f, 3.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(3.0f, -4.0f, 3.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(3.0f, -4.0f, -3.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-3.0f, -4.0f, -3.0f);
glEnd();
glfwSwapBuffers();
}
请注意,如果我将 OnInit() 中对 load_texture_TGA 的调用替换为对 glfwLoadTexture2D() 的调用,那么我就会遇到仅显示第一个纹理的问题。纹理并没有像他们应该的那样显示是怎么回事?
PS对不起,如果这真的很长,我习惯于在寻求编码帮助时提供我认为相关的所有信息。