c++ - 纹理不会在 OpenGL 和 Xcode 上显示

标签 c++ xcode opengl

我的纹理不会显示在屏幕上,只会显示彩色方 block 。 我配置了一个条件和一个 cout 语句,让我知道图像是否正确加载到内存中。确实如此,但纹理不会显示为什么会发生这种情况?有什么我想念的吗?

这是我的代码:

标题:Image.hpp

#ifndef IMAGEN_HPP_INCLUDED
#define IMAGEN_HPP_INCLUDED

// Especificacion de Image que nos ayudara a cargar y mostrar las imagenes
class Image
{
private:
   unsigned char* Data;
   int ancho;
    int alto;

 public:

// Constructor con parametros de Ancho y Alto que son de la imagen real y
// la direccion de la imagen
Image(int iw, int ih, const char* path)
{
    this->ancho = iw;
    this->alto = ih;

    CargarImagen(path);
}

// Funcion para cargar una imagen en memoria
int CargarImagen(const char* path)
{
    FILE *imagen = fopen(path, "r");
    int InfoTam = this->ancho * this->alto * 3;
    this->Data = (unsigned char*) malloc( InfoTam );

    if( imagen == NULL ){
        cout<<"ERROR"<<endl;
        return 0; }
    else{
        cout<<"Imagen cargada"<<endl;
    }

    fread(this->Data, InfoTam, 1, imagen);

    fclose(imagen);
    return 1;
}

// Dibujar la imagen que esta en memoria
void Dibujar(int x, int y, int w, int h)
{
    glColor3f(1.0f, 1.0f, 1.0f);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, this->ancho, this->alto, 0, GL_RGB, GL_UNSIGNED_BYTE, Data);

    glBegin(GL_QUADS);
        glTexCoord2f(0.0, 1.0); glVertex2f(x, y);
        glTexCoord2f(1.0, 1.0); glVertex2f(x + w, y);
        glTexCoord2f(1.0, 0.0); glVertex2f(x + w, y + h);
        glTexCoord2f(0.0, 0.0); glVertex2f(x, y + h);
    glEnd();
}
};

#endif

这是 main.cpp:

#include <iostream>
#include <GLUT/GLUT.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h> //New
#include <time.h>
#include <cstdio>
using namespace std;

#include "image.hpp"

#define pi 3.1415962
int ancho=700, alto=700;
float cx, cy=70; //Posición inicial. Donde empieza a caer la basura
int xpos, ypos, caida=1; //Posicion del mouse

int puntaje=0;

//Declaracion de imagenes

Image* BotePapel = new 
Image(256,256,"/Users/luissegovia/Documents/Recycle.raw");
Image* Fondo = new 
Image(2048,2048,"/Users/luissegovia/Documents/Background.raw");


//Funcion de display
void CodeFun(){
glClear(GL_COLOR_BUFFER_BIT);   //Limpia Buffer
glPointSize(100);               //Tamaño del punto

//----------
glEnable(GL_TEXTURE_2D);
BotePapel->Dibujar(3, 1, 10, 10);
//Fondo->Dibujar(0, 0, 700, 700);

//

glFlush();
}


int main(int argc, char *argv[]) {
//Parametros de inicio de OpenGL

glutInit(&argc,argv); //Inicializar libreria Glut
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); //Establece modo de 
visualizacion incial

glutInitWindowSize(ancho,alto); //Tamanio de ventana
glutInitWindowPosition(0,0);    //Posicion de la ventana
glutCreateWindow("Prototipo Juego 2D"); //Titulo de ventana
glClearColor(1,0.5,0.5,1);      //Color de fondo
gluOrtho2D(0,70,0,70);//Inicializacion del plano (1er cuadrante)

glutDisplayFunc(CodeFun);

glutMainLoop();
return 0;
}

最佳答案

您在未创建和绑定(bind)任何纹理的情况下调用 glTexImage2D()。虽然可以使用默认纹理 (glBindTexture(GL_TEXTURE_2D, 0)),但我不会依赖它。因此,改为创建一个这样的:

GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)

然后调用 glTexImage2D() 并执行您已经执行的操作。

但是不要在 CargarImagen() 中创建纹理,因为您不希望每一帧都创建一个新纹理。

因此与您当前的布局有关。在 main() 中的 gluOrtho2D() 之后创建纹理。但将 GLuint texture 保留为全局变量。然后一切都应该工作。

关于c++ - 纹理不会在 OpenGL 和 Xcode 上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43025496/

相关文章:

c++ - 编译器在初始化大型 std::arrays 时挂起

ios - 如何使用 MKMapView 绘制或制作自定义路线

c++ - 反转 x 轴的透视投影 (glm::perspective)

performance - OpenGL - 快速纹理四边形?

opengl - 笔记本电脑关闭屏幕时关闭屏幕渲染?

c++ - 扩展由接口(interface)派生的具体类的方法池

c++ - 动态使用 GetDriveType

c++ - 在 C++ 代码中处理 Angle Wrap

ios - 如何确保 Xcode 中的 iPhone 模拟器在我每次使用它时都能越过启动屏幕?

objective-c - uitableview单元格突出显示