I am new to openGL, I can’t find a solution to this problem. I have added a function to draw, main and fragment shader in which I think is the problem. I think the binding is done correctly. I had to remove some of the code but the rest is 100% correct.
void rysuj(void)
{
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID); //u┐yj programu, czyli naszego shadera
GLuint MVP_id = glGetUniformLocation(programID, "MVP"); // pobierz lokalizację zmiennej 'uniform' "MV" w programie
MV = glm::mat4(1.0f); //macierz jednostkowa
MV = glm::translate(MV, glm::vec3(0, -2, kameraD ));
MV = glm::scale(MV, glm::vec3(3.0, 3.0, 3.0));
MV = glm::rotate(MV, (float)glm::radians(kameraZ + 10), glm::vec3(1, 0, 0));
MV = glm::rotate(MV, (float)glm::radians(kameraX + 40), glm::vec3(0, 1, 0));
glm::mat4 MVP = P * MV;
glUniformMatrix4fv(MVP_id, 1, GL_FALSE, &(MVP[0][0]));
////////////////////////////////////////////////////
glUniform3f(objectColor_id, 1.0f, 0.5f, 0.3f);
glUniform3f(lightColor_id, 1.0f, 1.0f, 1.0f);
glUniform3f(lightPos_id,lightPos[0], lightPos[1], lightPos[2]);
/////////////////////////////////////////////////
// render the cube
glBindVertexArray(cubeVAO);
glDrawArrays(GL_QUADS, 0, 4);
glUseProgram(lamp_ID);
GLuint MVPlamp_id = glGetUniformLocation(lamp_ID, "MVP");
MV = glm::translate(MV, lightPos);
MV = glm::scale(MV, glm::vec3(0.1, 0.1, 0.1));
MV = glm::rotate(MV, (float)glm::radians(-30.0), glm::vec3(0, 1, 0));
MVP = P * MV;
glUniformMatrix4fv(MVPlamp_id, 1, GL_FALSE, &(MVP[0][0]));
glBindVertexArray(lightVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glFlush();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glEnable(GL_DEPTH_TEST);
programID = loadShaders("vertex_shader.glsl", "fragment_shader.glsl");
glUseProgram(programID);
objectColor_id = glGetUniformLocation(programID, "objectColor");
lightColor_id = glGetUniformLocation(programID, "lightColor");
lightPos_id = glGetUniformLocation(programID, "lightPos");
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glGenBuffers(1, &VBO2);
glBindBuffer(GL_ARRAY_BUFFER, VBO2);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);
glGenVertexArrays(1, &cubeVAO);
glBindVertexArray(cubeVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO2);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// normal attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// second, configure the light's VAO (VBO stays the same; the vertices are the same for the light object which is also a 3D cube)
lamp_ID = loadShaders("lamp_vshader.glsl", "lamp_fshader.glsl");
glGenVertexArrays(1, &lightVAO);
glBindVertexArray(lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// note that we update the lamp's position attribute's stride to reflect the updated buffer data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glutMainLoop(); // start
glDeleteVertexArrays(1, &cubeVAO);
glDeleteVertexArrays(1, &lightVAO);
glDeleteBuffers(1, &VBO);
return(0);
}
#version 330
out vec4 FragColor;
in vec3 Normal;
in vec3 FragPos;
in vec2 TexCoords; // Przekaż współrzędne tekstury
uniform vec3 lightPos;
uniform vec3 lightColor;
uniform vec3 objectColor;
uniform sampler2D tex0; // Przekaż identyfikator tekstury
void main()
{
viewPos=vec3(-1.0f, -1.0f, 2.0f);
vec3 textureColor = texture(tex0, TexCoords);
// ambient
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;
// diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
// specular
float specularStrength =0.5;
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * lightColor;
vec3 result = (ambient + diffuse + specular) * textureColor;
FragColor = vec4(result, 1.0);
}
I don’t have any more ideas on how to fix it. Thanks in advance.
New contributor
marpls is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.