因此,我使用GLFW進行窗口管理,並且我使用GLEW進行擴展管理。我正在嘗試寫一個我的兄弟提出的跨平臺遊戲。這是一款紙牌遊戲。但是,爲了獲得3D表面上紋理貼圖和渲染的基礎知識,我試圖獲取一些卡片紋理以顯示在立方體的兩側。我的第一個問題是使用glfw的TGA加載程序,其中只有第一個要加載的紋理可以顯示(雖然它會在我嘗試紋理的每個表面上正確顯示),儘管已經使用glGenTextures的GLuint指針數組。然後,我發現http://home.comcast.net/~mlyn7576/OpenGL-TGA/,它也似乎工作,但現在只顯示最後一個加載的紋理,並且它在所有面上顯示一個紋理,即使我只引用它的GL名稱一次。那麼我做錯了什麼?我無法理解爲什麼只顯示一個加載的紋理。在OpenGL多維數據集上顯示多個紋理,C++ GLFW
這裏是我的主頭:
#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();
}
需要注意的是,如果我是來取代在兩個電話在調用glfwLoadTexture2D()的OnInit()中調用load_texture_TGA,這就是我只能得到冷杉問題的地方st紋理顯示。發生什麼事情,紋理不會像他們應該顯示的那樣顯示?
P.S.對不起,如果這真的很長,我習慣於在尋求編碼幫助時提供所有我認爲合適的信息。
非常感謝你。我試過做glEnd().. glBegin()部分,但由於某種原因,我從來沒有嘗試過將glBindTexture調用放在兩者之間。它終於有效!現在我只需要弄清楚如何保持紋理不被打印在表面的兩側。 – Malkierian 2012-07-20 17:21:15