- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在下面的测试程序中,我试图在屏幕 (500 x 500) 的左下角渲染一个绿色方 block (250 x 250)。
我使用两个缓冲区纹理将两个 int 数组(每个数组的大小为 500 * 500)传递给片段着色器。这些数组只是简单地填充了对应于屏幕空间中所有 (x, y) 坐标的 x(第一个数组)和 y(第二个数组)值(这在下面代码的 init() 部分中显示)。
在片段着色器中,如果当前片段位置(在屏幕空间中)在 x 和 y 方向上都小于 250.0,则该片段为绿色。否则,它是红色的。 图片如下所示。
下面是完整的 OpenGL 代码,以及直通顶点着色器(位于主程序中)和片段着色器(从文件中读入)。在片段着色器中,有三个测试(A、B 和 C)。 测试 B 和 C 未按预期工作。他们在右下方而不是左下方绘制绿色方 block 。
测试 A(正确):当我在条件 if 检查中使用 gl_FragCoord.xy 时,此代码的输出按预期工作(屏幕左下角的绿色方 block )。
测试 B(不正确):如果我使用缓冲区纹理和 texelFetch 来检索当前片段的 x 和 y 值,然后在条件 if 检查中使用它,绿色方 block 得到绘制在右下角。
测试 C(不正确):如果我放弃缓冲区纹理,而只是计算当前片段索引并使用简单的模和除法来获取 x、y 索引,则绿色方 block 是仍然绘制在右下角。
如有任何见解,我们将不胜感激。
我正在使用:
Ubuntu 12.04.5 LTS, 64 bit
glGetString(GL_VERSION) = 4.4.0 NVIDIA 331.113
主程序:mytest.cc
// This program was modified from:
// https://www.opengl.org/discussion_boards/showthread.php/173917-samplerBuffer-example-needed
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#define GL_GLEXT_PROTOTYPES 1
#include <GL/gl.h>
#include <GL/glut.h>
using namespace std;
static const char Vertex_src[] =
"void main(void) \n"
"{ \n"
" gl_Position = ftransform(); \n"
"} \n";
std::string readFile(const char *filePath)
{
std::string content;
std::ifstream fileStream(filePath, std::ios::in);
if(!fileStream.is_open()) {
std::cerr << "Could not read file " << filePath << ". File does not exist." << std::endl;
return "";
}
std::string line = "";
while(!fileStream.eof()) {
std::getline(fileStream, line);
content.append(line + "\n");
}
fileStream.close();
return content;
}
void keybd ( unsigned char, int, int )
{
exit ( 0 ) ;
}
void reshape(int wid, int ht)
{
glViewport(0, 0, wid, ht);
}
void showGLerror ()
{
GLenum err ;
while ( (err = glGetError()) != GL_NO_ERROR )
fprintf ( stderr, "OpenGL Error: %s\n", gluErrorString ( err ) ) ;
}
void display ( void )
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, 500, 500);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
// Draw a full screen quad.
GLfloat s = 1.0;
glBegin(GL_QUADS);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(s, 0.0f, 0.0f);
glVertex3f(s, s, 0.0f);
glVertex3f(0.0f, s, 0.0f);
glEnd();
glPopAttrib();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glutSwapBuffers () ;
glutPostRedisplay () ;
}
void showShaderInfo ( const char *what, GLuint handle )
{
int len = 0 ;
glGetObjectParameterivARB ( handle, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len ) ;
if ( len > 0 )
{
int trueLen ;
char *s = new char [ len ] ;
glGetInfoLogARB ( handle, len, &trueLen, s ) ;
if ( trueLen > 0 && s [ 0 ] != '\0' )
fprintf ( stderr, "%s:\n%s\n", what, s ) ;
delete [] s ;
}
}
GLuint compileShader ( const char *src, GLenum type )
{
const char *type_str = type == GL_VERTEX_SHADER ? "vertex" : "fragment";
GLuint handle = glCreateShader( type ) ;
glShaderSource ( handle, 1, &src, 0 ) ;
glCompileShader( handle ) ;
GLint compiled ;
glGetShaderiv( handle, GL_COMPILE_STATUS, &compiled ) ;
if ( !compiled )
{
showShaderInfo ( type_str, handle ) ;
fprintf ( stderr, "Failed to compile %s shader.\n", type_str );
exit ( 1 ) ;
}
return handle ;
}
GLuint linkShaders ( GLuint vsHandle, GLuint fsHandle )
{
GLint linked ;
GLuint handle = glCreateProgram() ;
glAttachShader ( handle, vsHandle ) ;
glAttachShader ( handle, fsHandle ) ;
glLinkProgram ( handle ) ;
glGetProgramiv ( handle, GL_LINK_STATUS, & linked ) ;
if ( !linked )
{
showShaderInfo ( "Linking", handle ) ;
fprintf ( stderr, "Failed to link shader program.\n" ) ;
exit ( 1 ) ;
}
return handle ;
}
void init()
{
int arraySize = 500 * 500;
int *array_x = new int[arraySize];
int *array_y = new int[arraySize];
// Populate arrays.
for (int y = 0; y < 500; y++) {
for (int x = 0; x < 500; x++) {
array_x[(y * 500) + x] = x;
array_y[(y * 500) + x] = y;
}
}
const size_t size = sizeof( int ) * arraySize;
//// array_x
//
// Generate and fill buffer object
GLuint buffer;
glGenBuffers ( 1, &buffer );
glBindBuffer ( GL_TEXTURE_BUFFER, buffer );
glBufferData ( GL_TEXTURE_BUFFER, size, array_x, GL_STATIC_DRAW ); // Alloc & Fill
// Generate texture "wrapper" around buffer object
GLuint tex;
glGenTextures ( 1, &tex );
glActiveTexture( GL_TEXTURE0);
glBindTexture ( GL_TEXTURE_BUFFER, tex );
glTexBuffer ( GL_TEXTURE_BUFFER, GL_R32I, buffer );
//// array_y
//
// Generate and fill buffer object
GLuint buffer2;
glGenBuffers ( 1, &buffer2 );
glBindBuffer ( GL_TEXTURE_BUFFER, buffer2 );
glBufferData ( GL_TEXTURE_BUFFER, size, array_y, GL_STATIC_DRAW ); // Alloc & Fill
// Generate texture "wrapper" around buffer object
GLuint tex2;
glGenTextures ( 1, &tex2 );
glActiveTexture( GL_TEXTURE0 + 1);
glBindTexture ( GL_TEXTURE_BUFFER, tex2 );
glTexBuffer ( GL_TEXTURE_BUFFER, GL_R32I, buffer2 );
}
int main ( int argc, char **argv )
{
// Init GL context
glutInit ( &argc, argv ) ;
glutInitDisplayMode ( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE ) ;
glutInitWindowSize ( 500, 500 ) ;
glutCreateWindow ( "Shader Test" ) ;
glutDisplayFunc ( display ) ;
glutKeyboardFunc ( keybd ) ;
glutReshapeFunc ( reshape ) ;
// Create buffer object and its texture buffer object wrapper
init();
// Load and compile shaders
printf( "Compiling vertex shader...\n" );
GLuint vsHandle = compileShader ( Vertex_src, GL_VERTEX_SHADER );
printf( "Compiling fragment shader...\n" );
GLuint fsHandle = compileShader ( (readFile("mytest.glsl")).c_str(), GL_FRAGMENT_SHADER);
// Link shaders
printf( "Linking...\n" );
GLuint handle = linkShaders ( vsHandle, fsHandle ) ;
// Activate shader
glUseProgram( handle ) ;
// Populate uniform
// (buffer texture will be on TEXUNIT 0)
glUniform1i( glGetUniformLocation( handle, "tex" ), 0 );
// (buffer2 texture will be on TEXUNIT 1)
glUniform1i( glGetUniformLocation( handle, "tex2" ), 1 );
// Draw with shader
glutMainLoop () ;
return 0 ;
}
片段着色器:mytest.glsl
#version 130
#extension GL_EXT_gpu_shader4: enable
uniform isamplerBuffer tex;
uniform isamplerBuffer tex2;
void main(void)
{
int width = 500;
// Get the current screen index we are going to work on.
// (Used for TEST B and TEST C below.)
int index = int((gl_FragCoord.y * float(width)) + gl_FragCoord.x);
// TEST A: This works as expected.
/*if (gl_FragCoord.x < 250.0 && gl_FragCoord.y < 250.0) {
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}
else {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}*/
// TEST B: Use buffer textures to get the value of the arrays at
// the index computed above. This does not work as expected.
int x_i = int(texelFetch(tex, index).r);
int y_i = int(texelFetch(tex2, index).r);
if (x_i < 250 && y_i < 250) {
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}
else {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
// TEST C: Get the x, y screen space coordinates based on the index
// computed above. This does not work as expected.
/*int x_i = index % 500;
int y_i = index / 500;
if (x_i < 250 && y_i < 250) {
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}
else {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}*/
}
测试 A 的结果 Test A http://www.shilpigupta.com/stack/TestA.png
测试 B 和测试 C 的结果(另请注意图像右上角的绿线伪像。) Test B and Test C http://www.shilpigupta.com/stack/TestB.png
最佳答案
错误在于你如何计算指数:
int index = int((gl_FragCoord.y * float(width)) + gl_FragCoord.x);
此代码仅在 gl_FragCoord
的小数部分为零时才有效。请注意,OpenGL 的窗口空间是以像素中心位于半整数位置(“逗号 5”)的方式定义的。如果没有多重采样或其他花哨的设置,片段着色器将准确地针对像素中心调用,因此您所有的 gl_FragCoord
值都为 .5,这基本上会导致您观察到的 0.5 * 宽度像素偏移. (在 B 和 C 的情况下,绿色区域实际上不是正方形,因为顶部缺少一条高度线,因为您已经从 x_i=250 开始底线)。
正确的做法是
int index = int(gl_FragCoord.y) * width + int(gl_FragCoord.x);
关于opengl - 使用 1D 纹理缓冲区时 GLSL 的意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31174974/
在 OpenGL/ES 中,在实现渲染到纹理功能时,您必须小心,不要引起反馈循环(从正在写入的同一纹理中读取像素)。由于显而易见的原因,当您读取和写入纹理的相同像素时,行为是未定义的。但是,如果您正在
正如我们最终都知道的那样,规范是一回事,实现是另一回事。大多数错误是我们自己造成的,但有时情况并非如此。 我相信列出以下内容会很有用: GPU 驱动程序中当前已知的与最新版本的 OpenGL 和 GL
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。为了帮助澄清这个问题以便可以重新打开它,visit the help center
我正在学习 OpenGL,非常想知道与显卡的交互如何。 我觉得了解它是如何在图形驱动程序中实现的,会让我了解 opengl 的完整内部结构(通过这个我可以知道哪些阶段/因素影响我对 opengl 性能
我正在尝试绘制到大于屏幕尺寸(即 320x480)的渲染缓冲区 (512x512)。 执行 glReadPixels 后,图像看起来是正确的,除非图像的尺寸超过屏幕尺寸——在本例中,超过 320 水平
我正在 Windows 中制作一个 3D 小行星游戏(使用 OpenGL 和 GLUT),您可以在其中穿过一堆障碍物在太空中移动并生存下来。我正在寻找一种方法来针对无聊的 bg 颜色选项设置图像背景。
如果我想要一个包含 100 个 10*10 像素 Sprite 的 Sprite 表,是否可以将它们全部排成一排来制作 1,000*10 像素纹理?还是 GPU 对不那么窄的纹理表现更好?这对性能有什
这个问题在这里已经有了答案: Rendering 2D sprites in a 3D world? (7 个答案) 关闭 6 年前。 我如何概念化让图像始终面对相机。我尝试将三角函数与 arcta
是否可以在 OpenGL 中增加缓冲区? 假设我想使用实例化渲染。每次在世界上生成一个新对象时,我都必须用实例化数据更新缓冲区。 在这种情况下,我有一个 3 个 float 的缓冲区 std::v
有人可以向我解释为什么下面的代码没有绘制任何东西,但如果我使用 GL_LINE_LOOP 它确实形成了一个闭环吗? glBegin(GL_POLYGON); for(int i = 0; i <= N
正如标题所说,OpenGL 中的渲染目标是什么?我对 OpenGL 很陌生,我看到的所有网站都让我很困惑。 它只是一个缓冲区,我在其中放置稍后将用于渲染的东西吗? 如果您能提供一个很好的引用来阅读它,
当使用 OpenGL 1.4 固定功能多纹理时,每个纹理阶段的输出在传递到下一个阶段之前是否都固定在 [0, 1]? spec说(第 153 页): If the value of TEXTURE_E
我比较了 2 个函数 openGL ES 和 openGL gvec4 texelFetchOffset(gsampler2DArray sampler, ivec3 P, int lod, ivec
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 6年前关闭。 Improve this qu
那么当你调用opengl函数时,比如glDraw或者gLBufferData,是否会导致程序线程停止等待GL完成调用呢? 如果不是,那么 GL 如何处理调用像 glDraw 这样的重要函数,然后立即更
我正在尝试实现级联阴影贴图,当我想访问我的视锥体的每个分区的相应深度纹理时,我遇到了一个错误。 更具体地说,当我想选择正确的阴影纹理时会出现我的问题,如果我尝试下面的代码,我会得到一个像 this 中
我想为OpenGL ES和OpenGL(Windows)使用相同的着色器源。为此,我想定义自定义数据类型并仅使用OpenGL ES函数。 一种方法是定义: #define highp #define
我尝试用 6 个位图映射立方体以实现天空盒效果。我的问题是一个纹理映射到立方体的每个面。我已经检查了 gDEBugger,在立方体纹理内存中我只有一个 图像(因为我尝试加载六个图像)。 代码准备纹理:
在 OpenGL 中偏移深度的最佳方法是什么?我目前每个多边形都有索引顶点属性,我将其传递给 OpenGL 中的顶点着色器。我的目标是在深度上偏移多边形,其中最高索引始终位于较低索引的前面。我目前有这
我是一名优秀的程序员,十分优秀!