gpt4 book ai didi

c - GLSL统一 float 未正确传递

转载 作者:太空宇宙 更新时间:2023-11-04 04:33:56 26 4
gpt4 key购买 nike

我正在尝试为场景填充四边形创建着色器。但我似乎无法将统一的 float 传递给着色器。

在下面的示例中,我正在初始化 glut,创建/编译和链接着色器,将统一的 int 和 float 传递给着色器,然后让它们返回检查。 int asdf 工作正常,float qwer 表现异常。

如果我将 qwer 的值设置为 1.3,则制服设置为 -2.0,如果我将其设置为 1.2,则制服设置为 2.0。

#include <stdio.h>
#include <string.h>
#include <GL/glut.h>
#include <unistd.h>

int gw = 640, gh = 360;

void drawScene(){
//creating a screen filling quad
glBegin(GL_QUADS);

glTexCoord2f(0.0, 0.0f); glVertex2i(-1, 1);
glTexCoord2f(1.0, 0.0f); glVertex2i(1, 1);
glTexCoord2f(1.0, 1.0f); glVertex2i(1, -1);
glTexCoord2f(0.0, 1.0f); glVertex2i(-1, -1);

glEnd();

glutSwapBuffers();
}

void update(int value){
glutPostRedisplay();
glutTimerFunc(1000 / 30, update, 0);
}

int main(int argc, char** argv){

//shader source code

char *fraShdrStr = "\n\
uniform int asdf;\
uniform float qwer;\
void main(){\n\
vec2 p = gl_TexCoord[0].xy;\n\
gl_FragColor=vec4(p.x,qwer,float(asdf),1.0);\n\
}";
char *verShdrStr = "\n\
void main(){\n\
gl_Position=gl_ModelViewProjectionMatrix*gl_Vertex;\n\
gl_TexCoord[0]=gl_MultiTexCoord0;\n\
}";

size_t verShdrLen, fraShdrLen;
char errorBuffer[1024];
int errorLength;
int program, verShdr, fraShdr;
verShdrLen = strlen(verShdrStr);
fraShdrLen = strlen(fraShdrStr);

//initializing glut
glutInit(&argc, argv);
glutInitWindowSize(gw, gh);

glutCreateWindow("");

//creating, compiling and linking shaders
verShdr = glCreateShader(GL_VERTEX_SHADER);
fraShdr = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource(verShdr, 1, &verShdrStr, &verShdrLen);
glShaderSource(fraShdr, 1, &fraShdrStr, &fraShdrLen);

glCompileShader(verShdr);
glGetShaderInfoLog(verShdr, 1024, &errorLength, errorBuffer);
if(errorLength) printf("Vertex Shader Error:\n%s\n", errorBuffer);

glCompileShader(fraShdr);
glGetShaderInfoLog(fraShdr, 1024, &errorLength, errorBuffer);
if(errorLength) printf("Fragmen Shader Error:\n%s\n", errorBuffer);

program = glCreateProgram();

glAttachShader(program, verShdr);
glAttachShader(program, fraShdr);

glLinkProgram(program);
glGetProgramInfoLog(program, 1024, &errorLength, errorBuffer);
if(errorLength) printf("Linking Error:\n%s\n", errorBuffer);



glUseProgram(program);

//initializing variables to pass as uniform
int asdf = 9;
int asdf2;
float qwer = 1.0;
float qwer2;

//setting the uniform values
glUniform1i(glGetUniformLocation(program, "asdf"), asdf);
glGetUniformiv(program, glGetUniformLocation(program, "asdf"), &asdf2);
printf("%d\n", asdf2);

glUniform1f(glGetUniformLocation(program, "qwer"), qwer);
glGetUniformfv(program, glGetUniformLocation(program, "qwer"), &qwer2);
printf("%f\n", qwer2);


glutDisplayFunc(drawScene);
glutTimerFunc(1000/30, update, 0);

glutMainLoop();
}

最佳答案

你误解了整个画面。由于 OpenGL 3.0 使用 glBegin/glEnd 来绘制内容已被弃用。相反,您应该使用一种基于所谓的顶点数组的方法。查看this question获取示例代码片段。

关于c - GLSL统一 float 未正确传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33145695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com