OpenGL Drawing triangles at given world coordinates in modern opengl

Started by
1 comment, last by SuperVGA 3 years, 8 months ago

It's my first time I am asking a question here so I apologize in advance for any mistake.

In the past two days, I decided to work on a 2D lighting system for my game and I came across a youtube channel that explained the ray casting algorithm (https://www.youtube.com/watch?v=fc3nnG2CG8U).

I've managed to implement the algorithm and now I have to fill the visibility polygon of the light by drawing triangles using the calculated intersection points (expressed in world coordinates).

I've tried two similar approaches:

Drawing using a dynamic vertex buffer

Calculate the normalized device coordinates with matrices multiplication (https://stackoverflow.com/questions/8491247/c-opengl-convert-world-coords-to-screen2d-coords) on the CPU and then upload the calculated vertex to a dynamic vertex buffer

Drawing using a geometry shader

I did the same thing that I did in the previous approach but I transferred all the vertices calculation on the GPU side to increase the performance and used a geometry shader.

#version 420 core

layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;


uniform vec3 lightPos;
uniform vec3 v1Pos;
uniform vec3 v2Pos;

uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;

void main()
{
	vec4 clipSpacePos = projectionMatrix * (viewMatrix * vec4(lightPos, 1.0f));
	gl_Position = vec4(clipSpacePos.xyz / clipSpacePos.w, 1.0f);
	EmitVertex();

	clipSpacePos = projectionMatrix * (viewMatrix * vec4(v1Pos, 1.0f));
	gl_Position = vec4(clipSpacePos.xyz / clipSpacePos.w, 1.0f);
	EmitVertex();

	clipSpacePos = projectionMatrix * (viewMatrix * vec4(v2Pos, 1.0f));
	gl_Position = vec4(clipSpacePos.xyz / clipSpacePos.w, 1.0f);
	EmitVertex();
}

Neither of them has worked.

Am I just using the wrong approach?

Advertisement

Do you understand how model, view and projection matrices work?
I can recommend this: https://learnopengl.com/Getting-started/Coordinate-Systems

Depending on whether your buffer data is static or dynamic, it can even be feasible to have a single
uniform mat4 model_view_projection;

Which you only update when either of the three are changed.

This topic is closed to new replies.

Advertisement