Shader Uniform

This commit is contained in:
Tom
2021-06-07 16:29:10 +02:00
parent 31a34f0da2
commit 4956a0af5b
7 changed files with 19 additions and 4 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -112,9 +112,7 @@ int main(void){
} }
//Main window creation //Main window creation
GLFWwindow* window; GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGl practice", NULL, NULL);
window = glfwCreateWindow(640, 480, "OpenGl practice", NULL, NULL);
if (!window){ if (!window){
std::cout << "Window could not be created!" << std::endl; std::cout << "Window could not be created!" << std::endl;
glfwTerminate(); glfwTerminate();
@@ -123,9 +121,11 @@ int main(void){
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
glfwSwapInterval(1); //v-sync
//GLEW init //GLEW init
if (glewInit() != GLEW_OK) { if (glewInit() != GLEW_OK) {
std::cout << "GLEW Error" << std::endl; std::cout << "GLEW could not be initialized" << std::endl;
return -1; return -1;
} }
@@ -169,15 +169,30 @@ int main(void){
unsigned int shader = CreateShader(source.VertexSource, source.FragmentSource); unsigned int shader = CreateShader(source.VertexSource, source.FragmentSource);
glUseProgram(shader); glUseProgram(shader);
//need to bound a shader before this
GLCall(int location = glGetUniformLocation(shader, "u_Color")); //finding u_Color location
ASSERT(location != -1);
GLCall(glUniform4f(location, 0.8f, 0.3f, 0.8f, 1.0f));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0)); //unbinding the buffer GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0)); //unbinding the buffer
float r = 0.0f;
float increment = 0.05f;
//Main loop //Main loop
while (!glfwWindowShouldClose(window)){ while (!glfwWindowShouldClose(window)){
/* Render here */ /* Render here */
GLCall(glClear(GL_COLOR_BUFFER_BIT)); GLCall(glClear(GL_COLOR_BUFFER_BIT));
GLCall(glUniform4f(location, r, 0.3f, 0.8f, 1.0f));
GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr)); GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr));
if (r > 1.0f)
increment = -0.05f;
else if (r < 0.0f)
increment = 0.05f;
r += increment;
/* Swap front and back buffers */ /* Swap front and back buffers */
GLCall(glfwSwapBuffers(window)); GLCall(glfwSwapBuffers(window));