0

I'm studying OpenGL with GLUT and GLEW library. But I have a question about setting uniform values into the program's fragment shader.

Here's a part of the fragment shader code.

#version 110 
uniform vec4 cameraPosition;
uniform vec4 lightPosition;

varying vec4 fragColor;
varying vec4 fragNormal;
varying vec4 fragPosition;

void main() {
    vec4 L = lightPosition - fragPosition;
    vec4 V = cameraPosition - fragPosition;

What I want to do is set 'cameraPosition' and 'lightPosition' uniform variables. Also, in the main program, I put some code to get these variables ID and pass some values into the variables like below.

GLuint cameraPositionID = glGetUniformLocation(programID, "cameraPosition");
GLuint lightPositionID = glGetUniformLocation(programID, "lightPosition");
GLfloat camPos[4] = { 1, 1, -3, 1 };
GLfloat lightPos[4] = { 1, 0, 0, 1 };
glUniform4fv(cameraPositionID, 4, &cameraPos[0]);
glUniform4fv(lightPositionID, 4, &lightPos[0]);

So, I try to pass camPos into cameraPosition, and lightPos into lightPosition. is it correct or incorrect? If you think incorrect, Do you have any idea about this?

0

1 Answer 1

2

The count parameter of glUniform4fv denotes how many elements of an uniform array you are going to set - in this case, how many vec4s. You don't have an array, so count must be one. According to the spec, your command should result in a GL_INVALID_OPERATION error, and not in setting any uniform values. (Apart from that, using count 4 would also read 16 floats from the memory, while your array is only 4 floats big).

It is also unclear if you did a glUspeProgram(programID) before you try to set these uniforms. As uniforms are per-program state, the glUniform*() commands apply only to the currently bound program.

Sign up to request clarification or add additional context in comments.

1 Comment

okay, It's now clear after I put 1 into the count, instead of 4. Much thanks :) :D:D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.