Furtrum and quad.

This commit is contained in:
CRy386i 2025-01-16 14:06:54 +02:00
parent 238882257b
commit c646198aa8
2 changed files with 67 additions and 34 deletions

97
main.c
View file

@ -14,6 +14,8 @@ SDL_GLContext glcontext = NULL;
GLuint program; GLuint program;
GLuint vbo; GLuint vbo;
GLuint ibo;
GLuint projMatrLoc;
static double FPS = INITIAL_FPS; static double FPS = INITIAL_FPS;
static double FrameTime = 1.0 / INITIAL_FPS; static double FrameTime = 1.0 / INITIAL_FPS;
@ -34,8 +36,32 @@ static void SetTitle ( SDL_Window *window )
SDL_SetWindowTitle(window, title); SDL_SetWindowTitle(window, title);
} }
float toRad ( float deg )
{
/* 180 deg = pi */
/* "deg" deg = x */
return deg * 3.141593f / 180.0f;
}
void resizeWindow ( int width, int height ) void resizeWindow ( int width, int height )
{ {
float n = 1.0f; // near
float f = 3.0f; // far
float fov = toRad(90); // 90 or 65 for console
float aspect = width / (float) height;
float t = n * tanf (fov / 2.0f);
float r = t * aspect;
float projMatr[] = {
n/r, 0, 0, 0,
0, n/t, 0, 0,
0, 0, (f+n)/(n-f), -1,
0, 0, 2.0f*n*f/(n-f), 0
}; // it's not rows, it's columns, 'GL_FALSE'
SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "n=%.1f f=%.1f fov=%.2f aspect=%.2f t=%.2f r=%.2f\n", n, f, fov, aspect, t, r);
glUseProgram(program);
glUniformMatrix4fv(projMatrLoc, 1, GL_FALSE, projMatr);
glUseProgram(0);
glViewport(0, 0, (GLsizei)width, (GLsizei)height); glViewport(0, 0, (GLsizei)width, (GLsizei)height);
} }
@ -144,32 +170,36 @@ void createProgram ( GLuint *shaders, int len )
void createBuffer ( void ) void createBuffer ( void )
{ {
GLushort indices[] = {
0, 1, 2,
0, 2, 3
};
float vertices[] = { float vertices[] = {
-1.0f, -1.0f, 1.0f, 1.0f, // left-bottom -1.0f, -1.0f, -2.0f, 1.0f, // left-bottom
1.0f, -1.0f, 1.0f, 1.0f, // right-bottom -1.0f, 1.0f, -2.0f, 1.0f, // left-top
0.0f, 1.0f, -1.0f, 1.0f, // top 1.0f, 1.0f, -2.0f, 1.0f, // right-top
1.0f, -1.0f, -2.0f, 1.0f, // right-bottom
-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 // colours
1.0f, 0.0f, 0.0f, 1.0f, // red 1.0f, 0.0f, 0.0f, 1.0f, // red
0.0f, 1.0f, 0.0f, 1.0f, // green 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 1.0f, 0.0f, 1.0f, 1.0f, // purple
0.0f, 1.0f, 1.1f, 1.0f // aqua 1.0f, 1.0f, 0.0f, 1.0f // yellow
}; };
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);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
} }
void setUniformLocation ( void ) void setUniformLocation ( void )
{ {
/* offsetLoc = glGetUniformLocation(program, "_offset"); */ projMatrLoc = glGetUniformLocation(program, "projMatr");
} }
void startShaderProg ( void ) void startShaderProg ( void )
@ -203,9 +233,10 @@ void _GLBuffer( void )
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void *) (6*4*sizeof(float))); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void *) (4*4*sizeof(float)));
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
glDisableVertexAttribArray(0); glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1); glDisableVertexAttribArray(1);
@ -216,10 +247,10 @@ 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 delayThreshold = 2;
int r, g, b, alpha_s, depth_s, stencil_s, dbuffer, GLJ, GLN, GLAccel, GLP = 0; int r, g, b, alpha_s, depth_s, stencil_s, dbuffer, glVersionMa, glVersionMi, glAccel, glProfile = 0;
double sleep_time = 0; double sleepTime = 0;
double time_counter = 0; double timeCounter = 0;
bool loopShouldStop = false; bool loopShouldStop = false;
SDL_SetHint(SDL_HINT_RENDER_VSYNC, 0); SDL_SetHint(SDL_HINT_RENDER_VSYNC, 0);
@ -244,7 +275,7 @@ int main( int argc, char* argv[] )
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(); timeCounter = 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) {
@ -270,10 +301,10 @@ int main( int argc, char* argv[] )
} }
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));
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &GLP); SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &glProfile);
SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &GLAccel); SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &glAccel);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &GLJ); SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &glVersionMa);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &GLN); SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &glVersionMi);
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);
@ -282,8 +313,8 @@ int main( int argc, char* argv[] )
SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &stencil_s); SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &stencil_s);
SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &dbuffer); SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &dbuffer);
SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "OpenGL Versin: %d.%d, OpenGL Accelerated: %d, OpenGL Profile Mask: %d", SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "OpenGL Version: %d.%d, OpenGL Accelerated: %d, OpenGL Profile Mask: %d",
GLJ, GLN, GLAccel, GLP); glVersionMa, glVersionMi, glAccel, glProfile);
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", 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); r, g, b, alpha_s, depth_s, stencil_s, dbuffer);
@ -323,20 +354,20 @@ int main( int argc, char* argv[] )
panicError(); panicError();
} }
sleep_time = time_counter - GetTime(); sleepTime = timeCounter - GetTime();
if (sleep_time * 1000 > delay_threshold) { if (sleepTime * 1000 > delayThreshold) {
Uint32 ms = (Uint32)(sleep_time * 1000) - delay_threshold; Uint32 ms = (Uint32)(sleepTime * 1000) - delayThreshold;
SDL_Delay(ms); SDL_Delay(ms);
if (time_counter < GetTime()) { if (timeCounter < GetTime()) {
delay_threshold++; delayThreshold++;
SDL_Log("Slept too long. Increased threshold to %u ms", delay_threshold); SDL_Log("Slept too long. Increased threshold to %u ms", delayThreshold);
} }
} }
while (time_counter > GetTime()) { while (timeCounter > GetTime()) {
/* Waiting for the right moment. */ /* Waiting for the right moment. */
} }
time_counter += FrameTime; timeCounter += FrameTime;
} }
SDL_DestroyWindow(window); SDL_DestroyWindow(window);

View file

@ -3,10 +3,12 @@
layout(location = 0) in vec4 pos; layout(location = 0) in vec4 pos;
layout(location = 1) in vec4 colour; layout(location = 1) in vec4 colour;
uniform mat4 projMatr;
smooth out vec4 _colour; smooth out vec4 _colour;
void main() void main()
{ {
gl_Position = pos; gl_Position = projMatr * pos;
_colour = colour; _colour = colour;
} }