Added libm, PortView, Triangles, Orthographics View
Added libm into build system '-lm'. Edited shaders. Added dynamic resize window. Added another triangle and test depth with intersection triangles.
This commit is contained in:
parent
018a67ea3f
commit
238882257b
4 changed files with 90 additions and 34 deletions
2
Makefile
2
Makefile
|
@ -1,6 +1,6 @@
|
||||||
CC=gcc
|
CC=gcc
|
||||||
CFLAGS=-O1 -DDEBUG -g -ggdb -std=c23
|
CFLAGS=-O1 -DDEBUG -g -ggdb -std=c23
|
||||||
LIBS=`pkg-config sdl3 --cflags --libs` -lGL -lGLEW
|
LIBS=`pkg-config sdl3 --cflags --libs` -lGL -lGLEW -lm
|
||||||
|
|
||||||
OBJ = main.o
|
OBJ = main.o
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
#version 460
|
#version 330
|
||||||
|
|
||||||
out vec4 outputColor;
|
smooth in vec4 _colour;
|
||||||
|
|
||||||
|
out vec4 outputColour;
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
outputColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
|
outputColour = _colour;
|
||||||
}
|
}
|
||||||
|
|
105
main.c
105
main.c
|
@ -1,5 +1,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
|
@ -29,10 +30,15 @@ static double GetTime( void )
|
||||||
static void SetTitle ( SDL_Window *window )
|
static void SetTitle ( SDL_Window *window )
|
||||||
{
|
{
|
||||||
char title[48] = {0};
|
char title[48] = {0};
|
||||||
SDL_snprintf(title, sizeof(title), "Hello Triangle! Framelimit: %6.3f Hz", FPS);
|
SDL_snprintf(title, sizeof(title), "Hello Triangles! Framelimit: %6.3f Hz", FPS);
|
||||||
SDL_SetWindowTitle(window, title);
|
SDL_SetWindowTitle(window, title);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void resizeWindow ( int width, int height )
|
||||||
|
{
|
||||||
|
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
|
||||||
|
}
|
||||||
|
|
||||||
void panicError ( void )
|
void panicError ( void )
|
||||||
{
|
{
|
||||||
SDL_GL_DestroyContext(glcontext);
|
SDL_GL_DestroyContext(glcontext);
|
||||||
|
@ -131,7 +137,7 @@ void createProgram ( GLuint *shaders, int len )
|
||||||
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
|
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
|
||||||
GLchar *info = SDL_malloc(sizeof(GLchar) * (infoLen + 1));
|
GLchar *info = SDL_malloc(sizeof(GLchar) * (infoLen + 1));
|
||||||
glGetProgramInfoLog(program, infoLen, NULL, info);
|
glGetProgramInfoLog(program, infoLen, NULL, info);
|
||||||
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Link failed: %s.", info);
|
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Link failed: %s", info);
|
||||||
panicError();
|
panicError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,15 +145,33 @@ void createProgram ( GLuint *shaders, int len )
|
||||||
void createBuffer ( void )
|
void createBuffer ( void )
|
||||||
{
|
{
|
||||||
float vertices[] = {
|
float vertices[] = {
|
||||||
-1.0f, -1.0f, 0.0f,
|
-1.0f, -1.0f, 1.0f, 1.0f, // left-bottom
|
||||||
1.0f, -1.0f, 0.0f,
|
1.0f, -1.0f, 1.0f, 1.0f, // right-bottom
|
||||||
0.0f, 1.0f, 0.0f
|
0.0f, 1.0f, -1.0f, 1.0f, // top
|
||||||
|
|
||||||
|
-1.0f, 1.0f, 1.0f, 1.0f, // left-top
|
||||||
|
1.0f, 1.0f, 1.0f, 1.0f, // right-top
|
||||||
|
0.0f, -1.0f, -1.0f, 1.0f, // bottom
|
||||||
|
|
||||||
|
// colours
|
||||||
|
1.0f, 0.0f, 0.0f, 1.0f, // red
|
||||||
|
0.0f, 1.0f, 0.0f, 1.0f, // green
|
||||||
|
0.0f, 0.0f, 1.0f, 1.0f, // blue
|
||||||
|
|
||||||
|
1.0f, 1.0f, 0.0f, 1.0f, // yellow
|
||||||
|
1.0f, 0.0f, 1.0f, 1.0f, // purple
|
||||||
|
0.0f, 1.0f, 1.1f, 1.0f // aqua
|
||||||
};
|
};
|
||||||
glGenBuffers(1, &vbo);
|
glGenBuffers(1, &vbo);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setUniformLocation ( void )
|
||||||
|
{
|
||||||
|
/* offsetLoc = glGetUniformLocation(program, "_offset"); */
|
||||||
|
}
|
||||||
|
|
||||||
void startShaderProg ( void )
|
void startShaderProg ( void )
|
||||||
{
|
{
|
||||||
createBuffer();
|
createBuffer();
|
||||||
|
@ -161,19 +185,29 @@ void startShaderProg ( void )
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
glDeleteShader(shaders[i]);
|
glDeleteShader(shaders[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setUniformLocation();
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glDepthMask(GL_TRUE);
|
||||||
|
glDepthFunc(GL_LEQUAL);
|
||||||
|
glDepthRange(0.0f, 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _GLBuffer( void )
|
void _GLBuffer( void )
|
||||||
{
|
{
|
||||||
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
|
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClearDepth(1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
glUseProgram(program);
|
glUseProgram(program);
|
||||||
|
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void *) (6*4*sizeof(float)));
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
glDisableVertexAttribArray(0);
|
glDisableVertexAttribArray(0);
|
||||||
|
glDisableVertexAttribArray(1);
|
||||||
|
|
||||||
glUseProgram(0);
|
glUseProgram(0);
|
||||||
}
|
}
|
||||||
|
@ -183,36 +217,44 @@ int main( int argc, char* argv[] )
|
||||||
const int WIDTH = 640;
|
const int WIDTH = 640;
|
||||||
const int HEIGHT = 480;
|
const int HEIGHT = 480;
|
||||||
int delay_threshold = 2;
|
int delay_threshold = 2;
|
||||||
int r, g, b, GLJ, GLN = 0;
|
int r, g, b, alpha_s, depth_s, stencil_s, dbuffer, GLJ, GLN, GLAccel, GLP = 0;
|
||||||
double sleep_time = 0;
|
double sleep_time = 0;
|
||||||
double time_counter = 0;
|
double time_counter = 0;
|
||||||
bool loopShouldStop = false;
|
bool loopShouldStop = false;
|
||||||
|
|
||||||
SDL_SetHint(SDL_HINT_RENDER_VSYNC, 0);
|
SDL_SetHint(SDL_HINT_RENDER_VSYNC, 0);
|
||||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||||
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_Init failed: %s.", SDL_GetError());
|
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_Init failed: %s", SDL_GetError());
|
||||||
|
panicError();
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
|
if (!SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)) {
|
||||||
|
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Set atrribute SDL_GL_CONTEXT_PROFILE_MASK: %s", SDL_GetError());
|
||||||
|
panicError();
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
||||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
|
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
||||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
|
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
||||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
|
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
||||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
|
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
||||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||||
|
|
||||||
PerformancePeriod = 1.0 / SDL_GetPerformanceFrequency();
|
PerformancePeriod = 1.0 / SDL_GetPerformanceFrequency();
|
||||||
time_counter = GetTime();
|
time_counter = GetTime();
|
||||||
|
|
||||||
window = SDL_CreateWindow("Hello SDL", WIDTH, HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
|
window = SDL_CreateWindow("Hello SDL", WIDTH, HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
|
||||||
if (!window) {
|
if (!window) {
|
||||||
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_CreateWindow: %s.", SDL_GetError());
|
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_CreateWindow: %s", SDL_GetError());
|
||||||
panicError();
|
panicError();
|
||||||
}
|
}
|
||||||
|
|
||||||
glcontext = SDL_GL_CreateContext(window);
|
glcontext = SDL_GL_CreateContext(window);
|
||||||
if (!glcontext) {
|
if (!glcontext) {
|
||||||
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_GL_CreateContext: %s.", SDL_GetError());
|
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_GL_CreateContext: %s", SDL_GetError());
|
||||||
panicError();
|
panicError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -223,20 +265,27 @@ int main( int argc, char* argv[] )
|
||||||
glewExperimental = GL_TRUE;
|
glewExperimental = GL_TRUE;
|
||||||
GLenum err = glewInit();
|
GLenum err = glewInit();
|
||||||
if (err != GLEW_OK) {
|
if (err != GLEW_OK) {
|
||||||
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "glewInit: %s.", glewGetErrorString(err));
|
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "glewInit: %s", glewGetErrorString(err));
|
||||||
panicError();
|
panicError();
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
|
SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
|
||||||
err = SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &GLJ);
|
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &GLP);
|
||||||
|
SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &GLAccel);
|
||||||
|
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &GLJ);
|
||||||
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &GLN);
|
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &GLN);
|
||||||
SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &r);
|
SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &r);
|
||||||
SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &g);
|
SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &g);
|
||||||
SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &b);
|
SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &b);
|
||||||
|
SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &alpha_s);
|
||||||
|
SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depth_s);
|
||||||
|
SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &stencil_s);
|
||||||
|
SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &dbuffer);
|
||||||
|
|
||||||
SDL_Log("%s", SDL_GetError());
|
SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "OpenGL Versin: %d.%d, OpenGL Accelerated: %d, OpenGL Profile Mask: %d",
|
||||||
|
GLJ, GLN, GLAccel, GLP);
|
||||||
SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "Red size: %d, Green size: %d, Blue size: %d, OpenGL: %d.%d.", r, g, b, GLJ, GLN);
|
SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "Red size: %d, Green size: %d, Blue size: %d, Alpha size: %d, Depth size: %d, Stencil size: %d, DoubleBuffer: %d",
|
||||||
|
r, g, b, alpha_s, depth_s, stencil_s, dbuffer);
|
||||||
|
|
||||||
SDL_GL_SetSwapInterval(0);
|
SDL_GL_SetSwapInterval(0);
|
||||||
startShaderProg();
|
startShaderProg();
|
||||||
|
@ -247,6 +296,9 @@ int main( int argc, char* argv[] )
|
||||||
while (SDL_PollEvent(&e)) {
|
while (SDL_PollEvent(&e)) {
|
||||||
if (e.type == SDL_EVENT_QUIT)
|
if (e.type == SDL_EVENT_QUIT)
|
||||||
loopShouldStop = true;
|
loopShouldStop = true;
|
||||||
|
else if (e.type == SDL_EVENT_WINDOW_RESIZED) {
|
||||||
|
resizeWindow(e.window.data1, e.window.data2);
|
||||||
|
}
|
||||||
else if (e.type == SDL_EVENT_KEY_DOWN) {
|
else if (e.type == SDL_EVENT_KEY_DOWN) {
|
||||||
if (e.key.key == SDLK_X) {
|
if (e.key.key == SDLK_X) {
|
||||||
FPS -= 0.1;
|
FPS -= 0.1;
|
||||||
|
@ -255,8 +307,7 @@ int main( int argc, char* argv[] )
|
||||||
}
|
}
|
||||||
FrameTime = 1.0 / FPS;
|
FrameTime = 1.0 / FPS;
|
||||||
SetTitle(window);
|
SetTitle(window);
|
||||||
}
|
} else if (e.key.key == SDLK_C) {
|
||||||
else if (e.key.key == SDLK_C) {
|
|
||||||
FPS += 0.1;
|
FPS += 0.1;
|
||||||
FrameTime = 1.0 / FPS;
|
FrameTime = 1.0 / FPS;
|
||||||
SetTitle(window);
|
SetTitle(window);
|
||||||
|
@ -268,7 +319,7 @@ int main( int argc, char* argv[] )
|
||||||
|
|
||||||
_GLBuffer();
|
_GLBuffer();
|
||||||
if (!SDL_GL_SwapWindow(window)) {
|
if (!SDL_GL_SwapWindow(window)) {
|
||||||
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_GL_SwapWwindow: %s.", SDL_GetError());
|
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_GL_SwapWwindow: %s", SDL_GetError());
|
||||||
panicError();
|
panicError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,7 +329,7 @@ int main( int argc, char* argv[] )
|
||||||
SDL_Delay(ms);
|
SDL_Delay(ms);
|
||||||
if (time_counter < GetTime()) {
|
if (time_counter < GetTime()) {
|
||||||
delay_threshold++;
|
delay_threshold++;
|
||||||
SDL_Log("Slept too long. Increased threshold to %u ms.", delay_threshold);
|
SDL_Log("Slept too long. Increased threshold to %u ms", delay_threshold);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
vertex.glsl
10
vertex.glsl
|
@ -1,8 +1,12 @@
|
||||||
#version 460
|
#version 330
|
||||||
|
|
||||||
layout(location = 0) in vec3 pos;
|
layout(location = 0) in vec4 pos;
|
||||||
|
layout(location = 1) in vec4 colour;
|
||||||
|
|
||||||
|
smooth out vec4 _colour;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_Position = vec4(pos.x, pos.y, pos.z, 1.0);
|
gl_Position = pos;
|
||||||
|
_colour = colour;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue