triangle has now colors

This commit is contained in:
2026-04-13 01:32:31 +02:00
parent 64b27a6b84
commit 0c74cf691f
7 changed files with 46 additions and 18 deletions

View File

@@ -12,8 +12,12 @@ struct VertexBuffer {
glBindBuffer(GL_ARRAY_BUFFER, bufferId); glBindBuffer(GL_ARRAY_BUFFER, bufferId);
glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(Vertex), data, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(Vertex), data, GL_STATIC_DRAW);
// POSITION
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), offsetof(struct Vertex,x)); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,x));
// COLOR
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,r));
glBindVertexArray(0); glBindVertexArray(0);
}; };

View File

@@ -15,5 +15,6 @@ typedef float float32;
typedef double float64; typedef double float64;
struct Vertex { struct Vertex {
float32 x, y, z; float32 x, y, z; // position
float32 r, g, b, a; // color
}; };

View File

@@ -2,3 +2,4 @@
|Index|Description| |Index|Description|
|---|---| |---|---|
|0|Position| |0|Position|
|1|Color|

View File

@@ -1,7 +1,9 @@
#version 330 core #version 330 core
layout(location = 0) out vec4 color; layout(location = 0) out vec4 f_color;
in vec4 v_color;
void main() { void main() {
color = vec4(1.0f, 1.0f, 0.0f, 1.0f); f_color = v_color;
} }

View File

@@ -1,7 +1,11 @@
#version 330 core #version 330 core
layout(location = 0) in vec3 position; layout(location = 0) in vec3 a_position;
layout(location = 1) in vec4 a_color;
out vec4 v_color;
void main() { void main() {
gl_Position = vec4(position, 1.0f); gl_Position = vec4(a_position, 1.0f);
v_color = a_color;
} }

View File

@@ -31,7 +31,7 @@ GLuint Shader::compile(std::string shaderSource, GLenum shaderType) {
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length); glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char* message = new char[length]; char* message = new char[length];
glGetShaderInfoLog(id, length, &length, message); glGetShaderInfoLog(id, length, &length, message);
std::cout << "Shader Compilation Error" << message << std::endl; std::cout << "Shader Compilation Error [" << "None" << "] " << message << std::endl;
delete[] message; delete[] message;
return 0; return 0;
} }

View File

@@ -19,10 +19,12 @@ int main() {
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32); SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
uint32 flags = SDL_WINDOW_OPENGL; //| SDL_WINDOW_FULLSCREEN;
SDL_Window* window = SDL_CreateWindow( SDL_Window* window = SDL_CreateWindow(
"Cinecraft", "Cinecraft",
800, 600, 800, 600,
SDL_WINDOW_OPENGL flags
); );
SDL_GLContext context = SDL_GL_CreateContext(window); SDL_GLContext context = SDL_GL_CreateContext(window);
@@ -35,9 +37,9 @@ int main() {
std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl; std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl;
Vertex vertices[] = { Vertex vertices[] = {
Vertex{-0.5f, -0.5f, 0.0f}, Vertex{-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f},
Vertex{0.0f, 0.5f, 0.0f}, Vertex{0.0f, 0.5f, 0.0f, 0.0, 1.0f, 0.0f, 1.0f},
Vertex{0.5f, -0.5f, 0.0f}, Vertex{0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f},
}; };
uint32_t numVertices = 3; uint32_t numVertices = 3;
@@ -47,15 +49,16 @@ int main() {
Shader shader("shaders/basic.vert", "shaders/basic.frag"); Shader shader("shaders/basic.vert", "shaders/basic.frag");
shader.bind(); shader.bind();
const uint64 perfCounterFrequency = SDL_GetPerformanceFrequency();
uint64 lastCounter = SDL_GetPerformanceCounter();
float32 delta = 0.0f;
// WIREFRAME
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
bool running = true; bool running = true;
SDL_Event e; SDL_Event e;
while (running) { while (running) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_EVENT_QUIT) {
running = false;
}
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
@@ -65,6 +68,19 @@ int main() {
vertexBuffer.unbind(); vertexBuffer.unbind();
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
while (SDL_PollEvent(&e)) {
if (e.type == SDL_EVENT_QUIT) {
running = false;
}
}
const uint64 endCounter = SDL_GetPerformanceCounter();
const uint64 counterElapsed = endCounter - lastCounter;
delta = (static_cast<float32>(counterElapsed) / static_cast<float32>(perfCounterFrequency));
const auto FPS = static_cast<uint32>(static_cast<float32>(perfCounterFrequency) / static_cast<float32>(counterElapsed));
std::cout << "FPS: " << FPS << std::endl;
lastCounter = endCounter;
} }
SDL_GL_DestroyContext(context); SDL_GL_DestroyContext(context);