好的。我正在進入opengl 2.1(沒有固定功能的東西),我遇到了着色器的麻煩。我在我的着色器中聲明瞭統一變量,在我的程序中,我獲得了統一的位置,併爲它分配了一個具有菱形的值,但它似乎不起作用。Opengl統一變量不工作?
這是我的着色器
片段着色器: 的#Version 120
varying float color;
void
main()
{
gl_FragColor = vec4(color, 0, 0, 1);
}
和我的頂點着色器: 的#Version 120
attribute vec2 position;
varying float color;
uniform float pr_color;
void
main()
{
color = pr_color;
gl_Position = vec4(position, 0.0, 1.0);
}
這是我如何傳遞數據到着色器:
void
display()
{
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(g_program);
pos_loc = glGetAttribLocation(g_program, "position");
col_loc = glGetUniformLocation(g_program, "pr_color");
glUniform1f(col_loc, 1.0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(pos_loc);
glVertexAttribPointer(pos_loc, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glDrawArrays(GL_TRIANGLES, 0, 3);
glutPostRedisplay();
glutSwapBuffers();
}
,我不認爲這是必要的,但在任何情況下,我initgl功能
void
init()
{
// Set clear color to black
glClearColor(0.0,0.0,0.0,0.0);
vshader = createShader(GL_VERTEX_SHADER, "vertex.glsl");
fshader = createShader(GL_FRAGMENT_SHADER, "fragment.glsl");
g_program = createProgram(vshader, fshader);
// Create vbo and send it the vertex data
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
}
的事情是,我的着色器的工作,如果我硬編碼顏色分爲片段sahder,那麼三角形如果我將顏色傳遞給頂點着色器,它不起作用,三角形被繪製爲黑色,這很奇怪,因爲我的位置屬性起作用,但是我的顏色統一設置爲0.正如您所看到的,我在傳遞統一值之前設置useprogram()。
編輯:我將gluniform1i(col_loc,1)更改爲glUniformif(col_loc,1.0)。還不行的
編輯:我要添加着色器加載功能,以確保問題不是在着色器
GLuint
createShader (GLenum type, char* filename)
{
GLuint shader;
// Load file
FILE* file;
file = fopen(filename, "r");
if (file == NULL)
{
printf("Error reading file \n");
}
// Get Length
fseek(file, 0, SEEK_END);
long length = ftell(file);
fseek(file, 0, SEEK_SET);
// Get source
char* source;
source = malloc((size_t) length + 1);
if (source == NULL)
{
printf("Error alocating space for shader\n");
}
// Read file
fread(source, 1, length, file);
// Close file
fclose(file);
source[length] = '\n';
// Create shader, attach it's source and compile it
shader = glCreateShader(type);
glShaderSource(shader, 1, (const GLchar*)&source, &length);
free(source); // Free shader source, once it's attached
glCompileShader(shader);
// Check for errors
GLint shader_status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_status);
if (!shader_status) {
fprintf(stderr, "Failed to compile %s:\n", filename);
GLint log_length;
char *log;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);
log = malloc(log_length);
glGetShaderInfoLog(shader, log_length, NULL, log);
fprintf(stderr, "%s", log);
free(log);
glDeleteShader(shader);
return 0;
}
return shader;
}
當有在着色器的錯誤,程序實際打印錯誤日誌,所以我不認爲錯誤在這裏,但無論如何,在這裏。
編輯:程序鏈接代碼
GLuint
createProgram (GLuint vertexs, GLuint fragments)
{
GLint program_ok;
// Create program and attach to shaders
GLuint program = glCreateProgram();
glAttachShader(program, vertexs);
glAttachShader(program, fragments);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &program_ok);
if (!program_ok) {
fprintf(stderr, "Failed to link shader program:\n");
GLint log_length;
char *log;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length);
log = malloc(log_length);
glGetProgramInfoLog(program, log_length, NULL, log);
fprintf(stderr, "%s", log);
free(log);
glDeleteProgram(program);
return 0;
}
return program;
}
請告訴我,如果有什麼不對我的着色器/節目,我剛開始得到可編程管線的基礎知識,現在我甚至無法三角形
爲什麼你傳遞一個整數值,如果制服是浮動的? – 2015-04-02 18:59:43
只是在做了一些更改後複製了代碼,但我嘗試使用1.0f,1.0,但它不起作用。我也無法傳遞矢量數據,這就是爲什麼我試圖用簡單的浮動 – twkmz 2015-04-02 19:02:34
你確定你的着色器鏈接正確嗎?無法看到您檢查GL_LINK_STATUS。 – Andreas 2015-04-03 21:03:52