为什么glDrawPixel在这里不起作用
Why is glDrawPixels not working in here?
这是代码。输出是一个灰色的正方形 - 一直,无论输入是什么并且明显错误。我的目标是能够将所有像素存储在某个地方并显示它们,这样我就可以继续使用简单的光线追踪器,但我似乎无法弄清楚这个glDrawPixel的事情。
#include <stdlib.h>
#include <GL/glut.h >
using namespace std;
struct RGBType {
float r;
float g;
float b;
//float a;
};
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
RGBType *pixels = new RGBType[250*250];
for (int x = 0; x < 250; x++) {
for (int y = 0; y < 250; y++) {
pixels->r = 0;
pixels->g = 1;
pixels->b = 1;
//pixels->a = 200;
}
}
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,250,250,GL_RGB,GL_UNSIGNED_BYTE,pixels);
//glColor3f(1.0,1.0,1.0);
glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();
glutSwapBuffers();
}
void init(void)
{
//select clearing (background) color
glClearColor(0.0, 0.0, 0.0, 0.0);
//initialize viewing values
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
int main(int argc, char** argv)
{
//Initialise GLUT with command-line parameters.
glutInit(&argc, argv);
//Set Display Mode
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
//Set the window size
glutInitWindowSize(250,250);
//Set the window position
glutInitWindowPosition(100,100);
//Create the window
glutCreateWindow("Ray Tracer");
//Call init (initialise GLUT
init();
//Call "display" function
glutDisplayFunc(display);
//Enter the GLUT event loop
glutMainLoop();
return 0;
}
struct RGBType {
float r;
float g;
float b;
^^^^^^^^ all floats here...
//float a;
};
...
RGBType *pixels = new RGBType[250*250];
...
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,250,250,GL_RGB,GL_UNSIGNED_BYTE,pixels);
^^^^^^^^^^^^^^^^ wat
你告诉OpenGL将pixels
解释为一个unsigned char
数组,以四个字节为一组读取它们,并将每个四个字节的前三个字节用作RGB通道。
不要对OpenGL撒谎。 最后很少适合你。
请尝试GL_FLOAT
。
并首先创建一个实际的纹理对象。
并在尝试将数据上传到该纹理对象之前绑定该纹理对象。
并为多边形指定一些纹理坐标。
并在绘制多边形之前启用纹理。
像这样:
#include <GL/glut.h>
struct RGBType
{
float r;
float g;
float b;
};
GLuint tex = 0;
void init()
{
RGBType pixels[ 250*250 ];
RGBType* temp = pixels;
for (int x = 0; x < 250; x++)
{
for (int y = 0; y < 250; y++)
{
temp->r = 0;
temp->g = 1;
temp->b = 1;
temp++;
}
}
glGenTextures( 1, &tex );
glBindTexture( GL_TEXTURE_2D, tex );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 250, 250, 0, GL_RGB, GL_FLOAT, NULL );
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,250,250,GL_RGB,GL_FLOAT,pixels);
}
void display(void)
{
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2, 2, -2, 2, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3ub( 255, 255, 255 );
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, tex );
glBegin(GL_QUADS);
glTexCoord2i( 0, 0 );
glVertex2i( 0, 0 );
glTexCoord2i( 1, 0 );
glVertex2i( 1, 0 );
glTexCoord2i( 1, 1 );
glVertex2i( 1, 1 );
glTexCoord2i( 0, 1 );
glVertex2i( 0, 1 );
glEnd();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(250,250);
glutCreateWindow("Ray Tracer");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- 为什么二进制搜索在我的测试中不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- 为什么简单的算术减法在"if"条件下不起作用?
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 通过 get-Method 访问变量在类外不起作用
- 为什么 ulimit -v 在 Clang 的地址清理器下不起作用?
- G++ C++17 类模板参数推导在非常特殊的情况下不起作用
- 我的C++合并排序代码不起作用。我在这里错过了什么?
- C++ 互斥锁可以交叉方法/变量吗?如果是这样,为什么在这里不起作用?
- 不明白为什么动态编程在这里不起作用
- 为什么断言在这里不起作用
- 为什么 [] 的重载函数在这里不起作用?
- 为什么glDrawPixel在这里不起作用
- 在 qt 中添加 += -std=c++11 QMAKE_CXXFLAGS 到 .pro 文件中不起作用(在 Linux
- 每当启动函数 game() 时,get(c) 第一次在循环中不起作用(在第 90 行中)。