[win32, wip] simple GL triangle example

This commit is contained in:
martinfouilleul 2023-05-12 16:50:14 +02:00
parent 52538248d9
commit b6db5107a3
1 changed files with 163 additions and 165 deletions

View File

@ -1,165 +1,163 @@
/************************************************************//** /************************************************************//**
* *
* @file: main.cpp * @file: main.cpp
* @author: Martin Fouilleul * @author: Martin Fouilleul
* @date: 30/07/2022 * @date: 30/07/2022
* @revision: * @revision:
* *
*****************************************************************/ *****************************************************************/
#include<stdlib.h> #include<stdio.h>
#include<string.h> #include<string.h>
#define _USE_MATH_DEFINES //NOTE: necessary for MSVC #define _USE_MATH_DEFINES //NOTE: necessary for MSVC
#include<math.h> #include<math.h>
#define MG_INCLUDE_GL_API #define MG_INCLUDE_GL_API
#include"milepost.h" #include"milepost.h"
#define LOG_SUBSYSTEM "Main" unsigned int program;
unsigned int program; const char* vshaderSource =
"#version 430\n"
const char* vshaderSource = "attribute vec4 vPosition;\n"
"#version 430\n" "uniform mat4 transform;\n"
"attribute vec4 vPosition;\n" "void main()\n"
"uniform mat4 transform;\n" "{\n"
"void main()\n" " gl_Position = transform*vPosition;\n"
"{\n" "}\n";
" gl_Position = transform*vPosition;\n"
"}\n"; const char* fshaderSource =
"#version 430\n"
const char* fshaderSource = "precision mediump float;\n"
"#version 430\n" "void main()\n"
"precision mediump float;\n" "{\n"
"void main()\n" " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
"{\n" "}\n";
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n"; void compile_shader(GLuint shader, const char* source)
{
void compile_shader(GLuint shader, const char* source) glShaderSource(shader, 1, &source, 0);
{ glCompileShader(shader);
glShaderSource(shader, 1, &source, 0);
glCompileShader(shader); int err = glGetError();
if(err)
int err = glGetError(); {
if(err) printf("gl error: %i\n", err);
{ }
printf("gl error: %i\n", err);
} int status = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
int status = 0; if(!status)
glGetShaderiv(shader, GL_COMPILE_STATUS, &status); {
if(!status) char buffer[256];
{ int size = 0;
char buffer[256]; glGetShaderInfoLog(shader, 256, &size, buffer);
int size = 0; printf("shader error: %.*s\n", size, buffer);
glGetShaderInfoLog(shader, 256, &size, buffer); }
printf("shader error: %.*s\n", size, buffer); }
}
} int main()
{
int main() mp_init();
{
LogLevel(LOG_LEVEL_DEBUG); mp_rect rect = {.x = 100, .y = 100, .w = 800, .h = 600};
mp_window window = mp_window_create(rect, "test", 0);
mp_init();
//NOTE: create surface
mp_rect rect = {.x = 100, .y = 100, .w = 800, .h = 600}; mg_surface surface = mg_surface_create_for_window(window, MG_GL);
mp_window window = mp_window_create(rect, "test", 0);
//NOTE: init shader and gl state
//NOTE: create surface mg_surface_prepare(surface);
mg_surface surface = mg_surface_create_for_window(window, MG_BACKEND_GL);
GLuint vao;
//NOTE: init shader and gl state glGenVertexArrays(1, &vao);
mg_surface_prepare(surface); glBindVertexArray(vao);
GLuint vao; GLuint vertexBuffer;
glGenVertexArrays(1, &vao); glGenBuffers(1, &vertexBuffer);
glBindVertexArray(vao);
GLfloat vertices[] = {
GLuint vertexBuffer; -0.866/2, -0.5/2, 0, 0.866/2, -0.5/2, 0, 0, 0.5, 0};
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
GLfloat vertices[] = { glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
-0.866/2, -0.5/2, 0, 0.866/2, -0.5/2, 0, 0, 0.5, 0};
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); unsigned int vshader = glCreateShader(GL_VERTEX_SHADER);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); unsigned int fshader = glCreateShader(GL_FRAGMENT_SHADER);
program = glCreateProgram();
unsigned int vshader = glCreateShader(GL_VERTEX_SHADER); compile_shader(vshader, vshaderSource);
unsigned int fshader = glCreateShader(GL_FRAGMENT_SHADER); compile_shader(fshader, fshaderSource);
program = glCreateProgram();
glAttachShader(program, vshader);
compile_shader(vshader, vshaderSource); glAttachShader(program, fshader);
compile_shader(fshader, fshaderSource); glLinkProgram(program);
glAttachShader(program, vshader); int status = 0;
glAttachShader(program, fshader); glGetProgramiv(program, GL_LINK_STATUS, &status);
glLinkProgram(program); if(!status)
{
int status = 0; char buffer[256];
glGetProgramiv(program, GL_LINK_STATUS, &status); int size = 0;
if(!status) glGetProgramInfoLog(program, 256, &size, buffer);
{ printf("link error: %.*s\n", size, buffer);
char buffer[256]; }
int size = 0;
glGetProgramInfoLog(program, 256, &size, buffer); glUseProgram(program);
printf("link error: %.*s\n", size, buffer);
} mp_window_bring_to_front(window);
// mp_window_focus(window);
glUseProgram(program);
while(!mp_should_quit())
mp_window_bring_to_front(window); {
// mp_window_focus(window); mp_pump_events(0);
mp_event* event = 0;
while(!mp_should_quit()) while((event = mp_next_event(mem_scratch())) != 0)
{ {
mp_pump_events(0); switch(event->type)
mp_event event = {0}; {
while(mp_next_event(&event)) case MP_EVENT_WINDOW_CLOSE:
{ {
switch(event.type) mp_request_quit();
{ } break;
case MP_EVENT_WINDOW_CLOSE:
{ default:
mp_request_quit(); break;
} break; }
}
default:
break; mg_surface_prepare(surface);
}
} glClearColor(0.3, 0.3, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
mg_surface_prepare(surface);
static float alpha = 0;
glClearColor(0.3, 0.3, 1, 1); //f32 aspect = frameSize.x/frameSize.y;
glClear(GL_COLOR_BUFFER_BIT); f32 aspect = 800/(f32)600;
static float alpha = 0; GLfloat matrix[] = {cosf(alpha)/aspect, sinf(alpha), 0, 0,
//f32 aspect = frameSize.x/frameSize.y; -sinf(alpha)/aspect, cosf(alpha), 0, 0,
f32 aspect = 800/(f32)600; 0, 0, 1, 0,
0, 0, 0, 1};
GLfloat matrix[] = {cosf(alpha)/aspect, sinf(alpha), 0, 0,
-sinf(alpha)/aspect, cosf(alpha), 0, 0, alpha += 2*M_PI/120;
0, 0, 1, 0,
0, 0, 0, 1}; glUniformMatrix4fv(0, 1, false, matrix);
alpha += 2*M_PI/120;
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glUniformMatrix4fv(0, 1, false, matrix); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glDrawArrays(GL_TRIANGLES, 0, 3);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0); mg_surface_present(surface);
glDrawArrays(GL_TRIANGLES, 0, 3); mem_arena_clear(mem_scratch());
}
mg_surface_present(surface);
} mp_terminate();
mp_terminate(); return(0);
}
return(0);
}