This commit is contained in:
ktyl 2021-07-05 08:51:55 +01:00
parent 76cfb120e8
commit 3d23c46fbd
5 changed files with 37 additions and 6 deletions

View File

@ -1,7 +1,9 @@
#version 330 core #version 330 core
out vec4 FragColor; out vec4 FragColor;
uniform vec4 ourColor;
void main() void main()
{ {
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); FragColor = ourColor;
} }

View File

@ -1,7 +1,10 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
out vec4 vertexColor;
void main() void main()
{ {
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
vertexColor = vec4(0.5, 0.0, 0.0, 1.0);
} }

View File

@ -37,6 +37,12 @@ void gfxInit()
glewExperimental = GL_TRUE; glewExperimental = GL_TRUE;
glewInit(); glewInit();
// intiliased opengl
int availableAttributes;
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &availableAttributes);
//printf("max vertex attributes %d\n", availableAttributes);
} }
unsigned int compileShaderProgram() unsigned int compileShaderProgram()
@ -47,6 +53,8 @@ unsigned int compileShaderProgram()
unsigned int shaderProgram = glCreateProgram(); unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vs); glAttachShader(shaderProgram, vs);
glAttachShader(shaderProgram, fs); glAttachShader(shaderProgram, fs);
// TODO: check program linking success
glLinkProgram(shaderProgram); glLinkProgram(shaderProgram);
glDeleteShader(vs); glDeleteShader(vs);

View File

@ -3,6 +3,7 @@
#define GLEW_STATIC #define GLEW_STATIC
#include "GL/glew.h" #include "GL/glew.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h> #include <SDL2/SDL_opengl.h>
@ -11,7 +12,6 @@
void gfxInit(); void gfxInit();
SDL_Window* getWindow(); SDL_Window* getWindow();
SDL_GLContext* getContext(); SDL_GLContext* getContext();
GLuint compileShader(const char* path, GLenum type);
unsigned int compileShaderProgram(); unsigned int compileShaderProgram();

View File

@ -15,8 +15,13 @@ unsigned int indices[] = {
}; };
// forward declarations // forward declarations
// input
int checkQuit(); int checkQuit();
// time
float time();
int main() int main()
{ {
gfxInit(); gfxInit();
@ -24,8 +29,6 @@ int main()
unsigned int shaderProgram = compileShaderProgram(); unsigned int shaderProgram = compileShaderProgram();
// TODO: check program linking success
// vertex array object // vertex array object
unsigned int VAO; unsigned int VAO;
glGenVertexArrays(1, &VAO); glGenVertexArrays(1, &VAO);
@ -50,19 +53,34 @@ int main()
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0); // TODO: wtf glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0); // TODO: wtf
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glUseProgram(shaderProgram);
// render loop // render loop
while (!checkQuit()) while (!checkQuit())
{ {
glUseProgram(shaderProgram); // clear background
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// update uniforms
float time = (float)SDL_GetTicks() / 1000.0f;
float greenValue = (sin(time)/2.0f)+0.5f;
int vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor");
glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
// draw
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
} }
return 0; return 0;
} }
float time()
{
return (float)SDL_GetTicks() / 1000.0f; // ms / 1000.0 = seconds since start
}
int checkQuit() int checkQuit()
{ {
SDL_Event event; SDL_Event event;