用OpenGL和C++实现PNG图像的发布
我正在尝试开发一款游戏,但我遇到了一个问题。 我使用png图像,我创建了自己的sprite类 这是我加载png的方式: loadImage.cpp: 这是我的精灵课: csprite.cpp: 这是我的主要课程: main.cpp: 结果是: 但是如果我更改main.cpp中的显示函数和main函数,如下所示:用OpenGL和C++实现PNG图像的发布,c++,opengl,C++,Opengl,我正在尝试开发一款游戏,但我遇到了一个问题。 我使用png图像,我创建了自己的sprite类 这是我加载png的方式: loadImage.cpp: 这是我的精灵课: csprite.cpp: 这是我的主要课程: main.cpp: 结果是: 但是如果我更改main.cpp中的显示函数和main函数,如下所示: #include <iostream> #include <OpenGL/gl.h> #include "GLUT/glut.h" #include "loadI
#include <iostream>
#include <OpenGL/gl.h>
#include "GLUT/glut.h"
#include "loadImage.h"
#include "csprite.h"
CFrame fplay1;
CSprite splay1;
void reshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
//****** CHANGE ******//
splay1.drawNormal();
glFlush();
glutSwapBuffers();
}
void init() {
glClearColor(0,0,0,0);
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(50, 50);
glutInitWindowSize(500, 500);
glutCreateWindow("Hello OpenGL");
init();
//****** CHANGE ******//
fplay1.load("Sasuke_run002.png");
splay1.addframe(fplay1);
splay1.setPosition(100,100);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
结果是:
我不明白为什么会这样。我想以第二种形式使用我的sprite类如果我理解正确,我认为问题在于:
CSprite::CSprite() {
int nf=1;
this->estado=0;
this->posx=0;
this->posy=0;
this->sprite=new CFrame[nf];
this->nframes=nf;
this->cont=0;
}
您已将“帧数”nf设置为1,并使用一个条目创建了一个CFrame指针数组,但从未初始化该帧
我会先将nf=this->nframes=0,然后将this->sprite=NULL,直到一个帧添加到sprite。当然,在任何地方使用sprite之前,都需要检查sprite是否为null。如果我理解正确,我认为问题在于:
CSprite::CSprite() {
int nf=1;
this->estado=0;
this->posx=0;
this->posy=0;
this->sprite=new CFrame[nf];
this->nframes=nf;
this->cont=0;
}
您已将“帧数”nf设置为1,并使用一个条目创建了一个CFrame指针数组,但从未初始化该帧
我会先将nf=this->nframes=0,然后将this->sprite=NULL,直到一个帧添加到sprite。当然,在任何地方使用sprite之前,都需要检查sprite是否为null。我想它在这里:
void CSprite::draw() {
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glGenTextures(1, &this->sprite[estado].texture);
//printf("++++ %d",sprite[estado].texture);
glBindTexture( GL_TEXTURE_RECTANGLE_ARB, this->sprite[estado].texture );
//glTexParameteri( sprite[estado].texture, GL_TEXTURE_PRIORITY, 1);
/*
GLclampf priorities;
priorities=1;
GLfloat p=1;
glPrioritizeTextures(1,&sprite[estado].texture, &p);*/
// Write the 32-bit RGBA texture buffer to video memory
glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, this->sprite[estado].img.Width, this->sprite[estado].img.Height,
0, GL_RGBA, GL_UNSIGNED_BYTE, this->sprite[estado].img.Pixels );
在绘制之前,您正在覆盖此->精灵[estado]。纹理
取出
glGenTextures(1, &this->sprite[estado].texture);
及
我想这应该行得通。我想它就在这里:
void CSprite::draw() {
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glGenTextures(1, &this->sprite[estado].texture);
//printf("++++ %d",sprite[estado].texture);
glBindTexture( GL_TEXTURE_RECTANGLE_ARB, this->sprite[estado].texture );
//glTexParameteri( sprite[estado].texture, GL_TEXTURE_PRIORITY, 1);
/*
GLclampf priorities;
priorities=1;
GLfloat p=1;
glPrioritizeTextures(1,&sprite[estado].texture, &p);*/
// Write the 32-bit RGBA texture buffer to video memory
glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, this->sprite[estado].img.Width, this->sprite[estado].img.Height,
0, GL_RGBA, GL_UNSIGNED_BYTE, this->sprite[estado].img.Pixels );
在绘制之前,您正在覆盖此->精灵[estado]。纹理
取出
glGenTextures(1, &this->sprite[estado].texture);
及
我认为这应该是可行的。只是一个小细节:GLTEXAGE的第三个参数被标记为internal_format,而不是components——组件的数量是由于OpenGL-1.1的某些兼容机制,但OpenGL ES不支持它。内部格式为GL_RGBA、GL_ALPHA或类似格式。所以这里的颜色实际上就是你们现在所说的glcolors。format参数现在指定所传递数据的格式。目前,使用PNG图像文件,您的代码很好,但您应该将glcolors作为格式参数传递。@Daniel Vera:这方面运气好吗?您成功地解决了这个问题吗?只是一个小细节:glTexImage的第三个参数被标记为internal_format,而不是components-组件的数量是由于OpenGL-1.1的一些兼容机制,但OpenGL ES不支持它。内部格式为GL_RGBA、GL_ALPHA或类似格式。所以这里的颜色实际上就是你们现在所说的glcolors。format参数现在指定所传递数据的格式。目前,使用PNG图像文件,您的代码很好,但您应该将glcolors作为格式参数传递。@Daniel Vera:这方面运气好吗?你成功地解决了这个问题吗?
glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, this->sprite[estado].img.Width, this->sprite[estado].img.Height,
0, GL_RGBA, GL_UNSIGNED_BYTE, this->sprite[estado].img.Pixels );