在OpenGL多维数据集上显示多个纹理,c ++ GLFW

时间:2012-07-20 08:07:59

标签: c++ opengl textures glfw

所以,我正在使用GLFW进行窗口管理,我有GLEW用于扩展管理。我正在尝试编写一个我兄弟想出的跨平台游戏。这是一场纸牌游戏。但是,为了获得3D表面上纹理映射和渲染的基础知识,我试图在立方体的两侧显示一些卡片纹理。我的第一个问题是使用glfw的TGA加载器,其中只有第一个要加载的纹理是可显示的(虽然它可以在我试图纹理的每个表面上正确显示),尽管已经使用了gluenTextures的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();
}

请注意,如果我要通过调用glfwLoadTexture2D()来替换OnInit()中对load_texture_TGA的调用,那么我就会遇到只显示第一个纹理的问题。纹理并不像他们应该那样显示出来的是什么?

P.S。对不起,如果这真的很长,我习惯在寻求编码帮助时提供我认为相关的所有信息。

1 个答案:

答案 0 :(得分:3)

我认为你需要在一个单独的批次中做每个面孔:

glBindTexture(GL_TEXTURE_2D, cardBackId);
glBegin(GL_QUADS);
// front vertices..
glEnd();

glBindTexture(GL_TEXTURE_2D, airCardId);
glBegin(GL_QUADS);
// Right vertices..
glEnd();

// Other faces...

有几种方法可以让您在一个批次中渲染多维数据集:

  • 生成包含多维数据集所有面的单个纹理。然后为立方体的顶点提供适当的纹理坐标。 你可以使用2d纹理或3d纹理。
  • 绑定不同纹理单元寄存器中的所有纹理并编写像素着色器。