Commit 9b7549a3 authored by Baldur Paulwitz's avatar Baldur Paulwitz

update

- compute shader als eigenes programm hinzugefuegt
- mehr code zur klassenstruktur konvertiert
- update() und draw() in den loop aufgenommen
- Quad in der Theorie hinzugefügt

-> Problem: Quad wird noch nicht gezeichnet, könnt ihr euch gerne mal anschauen
parent 753aaf09
 mm_raytracer.cpp
glCalls.cpp
Code wird generiert...
 glCalls.cpp
mm_raytracer.vcxproj -> C:\Users\eleph\Documents\GitHub\mm_raytracer\VisualStudio\Debug\mm_raytracer.exe
B^C:\USERS\ELEPH\DOCUMENTS\GITHUB\MM_RAYTRACER\VISUALSTUDIO\MM_RAYTRACER\SRC\GLAD.C
......
B^C:\USERS\ELEPH\DOCUMENTS\GITHUB\MM_RAYTRACER\SRC\GLCALLS.CPP|C:\USERS\ELEPH\DOCUMENTS\GITHUB\MM_RAYTRACER\VISUALSTUDIO\MM_RAYTRACER\MM_RAYTRACER.CPP
......
B^C:\USERS\ELEPH\DOCUMENTS\GITHUB\MM_RAYTRACER\VISUALSTUDIO\MM_RAYTRACER\DEBUG\GLAD.OBJ|C:\USERS\ELEPH\DOCUMENTS\GITHUB\MM_RAYTRACER\VISUALSTUDIO\MM_RAYTRACER\DEBUG\GLCALLS.OBJ|C:\USERS\ELEPH\DOCUMENTS\GITHUB\MM_RAYTRACER\VISUALSTUDIO\MM_RAYTRACER\DEBUG\MM_RAYTRACER.OBJ
......
#version 430
layout(location = 0) in vec4 cPosition;
layout(location = 1) in vec4 cColor;
out vec4 vColor;
#define M_PI 3.141592653
//uniforms
uniform float angle_y;
uniform float angle_x;
//uniform mat4 MPVmatrix;
void main()
{
/*
//Some "good" perspective parameters:
float near = 1.0;
float far = 12.0;
float left = -2.0;
float right = 2.0;
float top = 2.0;
float bottom = -2.0; */
//Also translate by (x: 0.0, y: -3.0, z: -6.0)
float near = 1.0;
float far = 12.0;
float left = -2.0;
float right = 2.0;
float top = 2.0;
float bottom = -2.0;
mat4 frustum = mat4(
2.0 * near / (right - left), 0.0, 0.0, 0.0,
0.0, 2.0 * near / (top - bottom), 0.0, 0.0,
(right + left) / (right - left), (top + bottom) / (top - bottom), -(far + near) / (far - near), -1.0,
0.0, 0.0, -2.0 * far * near / (far - near), 0.0
);
mat4 rot_y = mat4(
cos(angle_y), 0.0, -sin(angle_y), 0.0,
0.0, 1.0, 0.0, 0.0,
sin(angle_y), 0.0, cos(angle_y), 0.0,
0.0, 0.0, 0.0, 1.0
);
mat4 rot_x = mat4(
1.0, 0.0, 0.0, 0.0,
0.0, cos(angle_x), -sin(angle_x), 0.0,
0.0, sin(angle_x), cos(angle_x), 0.0,
0.0, 0.0, 0.0, 1.0
);
vec4 trans = vec4(0, -3, -6, 1);
gl_Position = frustum * (rot_x *( rot_y * vPosition) + trans);
vColor = cColor;
}
......@@ -7,5 +7,5 @@ out vec4 fColor;
void main()
{
fColor = vColor;
fColor = vec4(1, 0, 1, 0);
}
......@@ -19,6 +19,13 @@
namespace rt
{
typedef struct
{
GLfloat position[3];
GLubyte color[3];
//TODO coordinates for texture
} VertexData;
class gl_rt_window
{
public:
......@@ -26,35 +33,35 @@ public:
gl_rt_window(GLFWwindow* window);
~gl_rt_window();
void update();
void draw();
private:
//Variables
GLFWwindow* window;
VertexData* rt_texture_quad;
GLuint compileShader(GLenum type, const char* shaderSource, std::string shaderTag);
char* readShaderFromFile(std::string filename);
GLboolean checkLinkedProgram(GLuint programObject);
GLboolean init();
void update();
void draw();
void teardown();
void key_callback(int key, int scancode, int action, int mods);
void error_callback(int error, const char* description);
};
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
typedef struct{
GLuint programObject;
GLuint programObjectNonCompute;
GLuint programObjectCompute;
GLuint vertexBufferObject;
GLuint vertexArrayObject;
GLuint vertexDataCount;
GLuint vertexArrayObject;
double time;
GLint height;
GLint width;
} UserData;
typedef struct
{
GLfloat position[3];
//TODO coordinates for texture
} VertexData;
}
#endif
......@@ -59,16 +59,17 @@ int main(int argc, char const* argv[])
//TODO register key callback (glfw)
//glfwSetKeyCallback(window, key_callback);
//main loop
while (!glfwWindowShouldClose(window))
{
//TODO do opengl stuff
rt_window->update();
rt_window->draw();
glfwPollEvents();
}
//TODO clean up
//teardown(window);
//clean up window
delete rt_window;
//clean up glfw
glfwDestroyWindow(window);
......
......@@ -43,6 +43,37 @@ namespace rt
return result;
}
GLboolean gl_rt_window::checkLinkedProgram(GLuint programObject)
{
// link status
GLint linked;
glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
checkError("glGetProgramiv");
if (!linked) {
GLint infoLen;
//get length of linker error message
glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char* errorMsg = new char[infoLen];
//get error message from program object
glGetProgramInfoLog(programObject, infoLen, NULL, errorMsg);
printf("Error linking program: %s\n", errorMsg);
}
glDeleteProgram(programObject);
return GL_FALSE;
}
return GL_TRUE;
}
GLuint gl_rt_window::compileShader(GLenum type, const char* shaderSource, std::string shaderTag) {
//shader handle
......@@ -98,6 +129,35 @@ namespace rt
UserData* userData = new UserData;
glfwSetWindowUserPointer(window, (void*)userData);
//set vertex data count
userData->vertexDataCount = 4;
//create the compute shader
char* computeShaderSource = readShaderFromFile((char*)"shader/compute.glsl");
GLuint computeShader = compileShader(GL_COMPUTE_SHADER, computeShaderSource, (char*)"Compute shader");
delete(computeShaderSource);
// hint to release resources of shader compiler
glReleaseShaderCompiler();
//program handle for compute shader
GLuint computeProgramObject;
computeProgramObject = glCreateProgram();
if (computeProgramObject == 0) return 0;
// attach shaders to program object
glAttachShader(computeProgramObject, computeShader);
if (!checkLinkedProgram(computeProgramObject))
{
return GL_FALSE;
}
userData->programObjectCompute = computeProgramObject;
//Create the vertex shader:
char* vertexShaderSource = readShaderFromFile((char*)"shader/vertex.glsl");
......@@ -149,32 +209,8 @@ namespace rt
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// link status
GLint linked;
// check link status
glGetProgramiv(programObject, GL_LINK_STATUS, &linked);
checkError("glGetProgramiv");
if (!linked) {
GLint infoLen;
//get length of linker error message
glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char* errorMsg = new char[infoLen];
//get error message from program object
glGetProgramInfoLog(programObject, infoLen, NULL, errorMsg);
printf("Error linking program: %s\n", errorMsg);
}
glDeleteProgram(programObject);
if (!checkLinkedProgram(programObject))
{
return GL_FALSE;
}
......@@ -183,7 +219,7 @@ namespace rt
checkError("glUseProgram");
// store program object in context
userData->programObject = programObject;
userData->programObjectNonCompute = programObject;
//initialize the modell
userData->time = glfwGetTime();
......@@ -202,6 +238,10 @@ namespace rt
glBindBuffer(GL_ARRAY_BUFFER, userData->vertexBufferObject);
checkError("glBindBuffer");
//set buffer data
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData) * userData->vertexDataCount, (const GLvoid*)rt_texture_quad, GL_STATIC_DRAW);
checkError("glBufferData");
//attribute index, component count, normalize, stride, pointer
glVertexAttribPointer(ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (GLvoid*)offsetof(VertexData, position));
checkError("glVertexAttribPointer - ATTRIB_POSITION");
......@@ -212,8 +252,8 @@ namespace rt
//TODO VertexData coords ndern
//attribute index, component count, normalize, stride, pointer
//glVertexAttribPointer(ATTRIB_COLOR, 3, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(VertexData), (GLvoid*)offsetof(VertexData, color));
//checkError("glVertexAttribPointer - ATTRIB_COLOR");
glVertexAttribPointer(ATTRIB_COLOR, 3, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(VertexData), (GLvoid*)offsetof(VertexData, color));
checkError("glVertexAttribPointer - ATTRIB_COLOR");
//enable vertex attribute
glEnableVertexAttribArray(ATTRIB_COLOR);
......@@ -238,6 +278,9 @@ namespace rt
{
UserData* userData = (UserData*)glfwGetWindowUserPointer(window);
glUseProgram(userData->programObjectNonCompute);
checkError("glUseProgram");
/*
//calculate time delta
double newTime = glfwGetTime();
......@@ -258,11 +301,11 @@ namespace rt
//update width and height
userData->height = height;
userData->width = width;
/*
// set viewport
glViewport(0, 0, width, height);
checkError("glViewport");
*/
//update uniforms TODO
//glUniform1f(userData->angleYLoc, userData->angleY);
//checkError("glUniform1f (angleY)");
......@@ -295,10 +338,6 @@ namespace rt
delete(userData);
}
void gl_rt_window::key_callback(int key, int scancode, int action, int mods) {
}
void gl_rt_window::error_callback(int error, const char* description) {
fprintf(stderr, "Error: %s\n", description);
}
......@@ -306,11 +345,34 @@ namespace rt
gl_rt_window::gl_rt_window(GLFWwindow* window)
{
this->window = window;
rt_texture_quad = new VertexData[4];
rt_texture_quad[0].position[0] = 1.0;
rt_texture_quad[0].position[1] = 1.0;
rt_texture_quad[0].position[2] = 0.0;
rt_texture_quad[1].position[0] = -1.0;
rt_texture_quad[1].position[1] = 1.0;
rt_texture_quad[1].position[2] = 0.0;
rt_texture_quad[2].position[0] = -1.0;
rt_texture_quad[2].position[1] = -1.0;
rt_texture_quad[2].position[2] = 0.0;
rt_texture_quad[3].position[0] = 1.0;
rt_texture_quad[3].position[1] = -1.0;
rt_texture_quad[3].position[2] = 0.0;
init();
}
gl_rt_window::~gl_rt_window()
{
delete[] rt_texture_quad;
teardown();
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment