我的纹理不会显示在屏幕上,只会显示彩色方 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/