Commit 753aaf09 authored by Baldur Paulwitz's avatar Baldur Paulwitz

class update

parent bfe32fa8
 glCalls.cpp
C:\Users\eleph\Documents\GitHub\mm_raytracer\src\glCalls.cpp(212,22): error C2039: "angleXLoc": Ist kein Element von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\include\glCalls.h(18): message : Siehe Deklaration von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\src\glCalls.cpp(214,2): error C2039: "angleYLoc": Ist kein Element von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\include\glCalls.h(18): message : Siehe Deklaration von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\src\glCalls.cpp(216,22): error C2039: "angleYLoc": Ist kein Element von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\include\glCalls.h(18): message : Siehe Deklaration von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\src\glCalls.cpp(218,2): error C2039: "angleYLoc": Ist kein Element von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\include\glCalls.h(18): message : Siehe Deklaration von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\src\glCalls.cpp(222,19): error C2039: "angleY": Ist kein Element von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\include\glCalls.h(18): message : Siehe Deklaration von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\src\glCalls.cpp(223,19): error C2039: "angleX": Ist kein Element von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\include\glCalls.h(18): message : Siehe Deklaration von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\src\glCalls.cpp(248,97): error C2039: "color": Ist kein Element von "VertexData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\include\glCalls.h(29): message : Siehe Deklaration von "VertexData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\src\glCalls.cpp(248,97): error C2618: ungültiger Memberbezeichner in offsetof
C:\Users\eleph\Documents\GitHub\mm_raytracer\src\glCalls.cpp(248,97): message : offsetof besitzt eine vordefinierte Bedeutung. Verwenden Sie "/Zc:offsetof-", um die alte, nicht konforme Definition wiederherzustellen.
C:\Users\eleph\Documents\GitHub\mm_raytracer\src\glCalls.cpp(300,33): error C2039: "angleYLoc": Ist kein Element von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\include\glCalls.h(18): message : Siehe Deklaration von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\src\glCalls.cpp(300,51): error C2039: "angleY": Ist kein Element von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\include\glCalls.h(18): message : Siehe Deklaration von "UserData"
C:\Users\eleph\Documents\GitHub\mm_raytracer\src\glCalls.cpp(300,51): error C2198: "PFNGLUNIFORM1FPROC": Nicht genügend Argumente für Aufruf.
 mm_raytracer.cpp
glCalls.cpp
Code wird generiert...
mm_raytracer.vcxproj -> C:\Users\eleph\Documents\GitHub\mm_raytracer\VisualStudio\Debug\mm_raytracer.exe
B^C:\USERS\ELEPH\SOURCE\REPOS\MM_RAYTRACER\MM_RAYTRACER\MM_RAYTRACER.CPP
......
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
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
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
......@@ -6,6 +6,7 @@
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <math.h>
#include <string>
#define ATTRIB_POSITION 0
#define ATTRIB_COLOR 1
......@@ -15,6 +16,31 @@
#define X_ROT_SPEED 10.0
*/
namespace rt
{
class gl_rt_window
{
public:
gl_rt_window() = delete;
gl_rt_window(GLFWwindow* window);
~gl_rt_window();
private:
//Variables
GLFWwindow* window;
GLuint compileShader(GLenum type, const char* shaderSource, std::string shaderTag);
char* readShaderFromFile(std::string filename);
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);
};
typedef struct{
GLuint programObject;
GLuint vertexBufferObject;
......@@ -30,11 +56,5 @@ typedef struct
GLfloat position[3];
//TODO coordinates for texture
} VertexData;
GLboolean init(GLFWwindow* window);
void update(GLFWwindow* window);
void draw(GLFWwindow* window);
void teardown(GLFWwindow* window);
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
void error_callback(int error, const char* description);
}
#endif
#include <string.h>
#include <stdio.h>
#include <iostream>
#include "../../include/glad/glad.h"
#include "include/glad/glad.h"
#include <GLFW/glfw3.h>
#include <fstream>
#include "include/glCalls.h"
int main(int argc, char const* argv[])
{
......@@ -53,7 +55,7 @@ int main(int argc, char const* argv[])
glfwSwapInterval(1);
//TODO init OpenGL stuff
rt::gl_rt_window* rt_window = new rt::gl_rt_window(window);
//TODO register key callback (glfw)
//glfwSetKeyCallback(window, key_callback);
......
......@@ -151,21 +151,21 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\src\glad.c" />
<ClCompile Include="..\..\src\glCalls.cpp" />
<ClCompile Include="mm_raytracer.cpp" />
<ClCompile Include="src\glad.c" />
<ClCompile Include="src\glCalls.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\glad\glad.h" />
<ClInclude Include="..\..\include\glCalls.h" />
<ClInclude Include="..\..\include\KHR\khrplatform.h" />
<ClInclude Include="..\..\include\sphere.h" />
<ClInclude Include="include\glad\glad.h" />
<ClInclude Include="include\glCalls.h" />
<ClInclude Include="include\KHR\khrplatform.h" />
<ClInclude Include="include\sphere.h" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\shader\compute.glsl" />
<None Include="..\..\shader\fragment.glsl" />
<None Include="..\..\shader\vertex.glsl" />
<None Include="packages.config" />
<None Include="Shader\compute.glsl" />
<None Include="Shader\fragment.glsl" />
<None Include="Shader\vertex.glsl" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
......
......@@ -27,36 +27,36 @@
<ClCompile Include="mm_raytracer.cpp">
<Filter>Quelldateien\src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\glad.c">
<ClCompile Include="src\glad.c">
<Filter>Quelldateien\src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\glCalls.cpp">
<ClCompile Include="src\glCalls.cpp">
<Filter>Quelldateien\src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\glad\glad.h">
<ClInclude Include="include\glCalls.h">
<Filter>Headerdateien\include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\KHR\khrplatform.h">
<ClInclude Include="include\sphere.h">
<Filter>Headerdateien\include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\sphere.h">
<ClInclude Include="include\glad\glad.h">
<Filter>Headerdateien\include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\glCalls.h">
<ClInclude Include="include\KHR\khrplatform.h">
<Filter>Headerdateien\include</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="..\..\shader\compute.glsl">
<None Include="Shader\compute.glsl">
<Filter>shader</Filter>
</None>
<None Include="..\..\shader\fragment.glsl">
<None Include="Shader\fragment.glsl">
<Filter>shader</Filter>
</None>
<None Include="..\..\shader\vertex.glsl">
<None Include="Shader\vertex.glsl">
<Filter>shader</Filter>
</None>
</ItemGroup>
......
#include "../include/glCalls.h"
#include <assert.h>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
void checkError(const char* errorText){
GLenum error = glGetError();
if(error != GL_NO_ERROR){
printf("GLError: %s - %d\n", errorText, error);
exit(0);
}
}
namespace rt
{
char* gl_rt_window::readShaderFromFile(std::string filename) {
char* result;
std::stringstream shaderCodeStream;
std::string codeLine;
std::ifstream shaderFile;
shaderFile.open(filename);
if (shaderFile.is_open())
{
while (getline(shaderFile, codeLine))
{
//TODO not sure if '\n' is necessary
shaderCodeStream << codeLine << '\n';
}
shaderFile.close();
}
std::string shaderCode = shaderCodeStream.str();
result = new char[shaderCode.length() + 1];
strcpy_s(result, shaderCode.length() + 1, shaderCode.c_str());
return result;
}
GLuint gl_rt_window::compileShader(GLenum type, const char* shaderSource, std::string shaderTag) {
//shader handle
GLuint shader;
//create empty shader object
shader = glCreateShader(type);
if (shader == 0) return 0;
//put source code into empty shader object
glShaderSource(shader, 1, &shaderSource, NULL);
//compile shader
glCompileShader(shader);
//compile status
GLint compiled;
//check compile status
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint infoLen = 0;
//get length of error message
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char* errorMsg = new char[infoLen];
//get error message
glGetShaderInfoLog(shader, infoLen, NULL, errorMsg);
std::cout << "Error compiling shader " << shaderTag;
printf(": %s\n", errorMsg);
}
// delete shader object
glDeleteShader(shader);
return GL_FALSE;
}
return shader;
}
GLboolean gl_rt_window::init() {
//allocate memory for user data
UserData* userData = new UserData;
glfwSetWindowUserPointer(window, (void*)userData);
//Create the vertex shader:
char* vertexShaderSource = readShaderFromFile((char*)"shader/vertex.glsl");
GLuint vertexShader = compileShader(GL_VERTEX_SHADER, vertexShaderSource, (char*)"Vertex shader");
delete(vertexShaderSource);
/*
//Create the vertex shader:
char* computeShaderSource = readShaderFromFile("shader/compute.glsl");
GLuint computeShader = compileShader(GL_COMPUTE_SHADER, computeShaderSource, "compute shader");
delete(computeShaderSource);
*/
//Create the fragment shader:
char* fragmentShaderSource = readShaderFromFile((char*)"shader/fragment.glsl");
GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource, (char*)"Fragment shader");
delete(fragmentShaderSource);
// hint to release resources of shader compiler
glReleaseShaderCompiler();
//program handle
GLuint programObject;
//create empty program object
programObject = glCreateProgram();
if (programObject == 0) return 0;
// attach shaders to program object
glAttachShader(programObject, vertexShader);
glAttachShader(programObject, fragmentShader);
// link shaders
glLinkProgram(programObject);
checkError("glLinkProgram");
// detach shaders from program object
glDetachShader(programObject, vertexShader);
glDetachShader(programObject, fragmentShader);
// delete shader objects
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);
return GL_FALSE;
}
// use program
glUseProgram(programObject);
checkError("glUseProgram");
// store program object in context
userData->programObject = programObject;
//initialize the modell
userData->time = glfwGetTime();
// create and bind dummy vao... it is needed in desktop OpenGL
glGenVertexArrays(1, &(userData->vertexArrayObject));
checkError("glGenVertexArray");
glBindVertexArray(userData->vertexArrayObject);
checkError("glBindVertexArray");
// generate a VBO
glGenBuffers(1, &(userData->vertexBufferObject));
checkError("glGenBuffers");
glBindBuffer(GL_ARRAY_BUFFER, userData->vertexBufferObject);
checkError("glBindBuffer");
//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");
//enable vertex attribute
glEnableVertexAttribArray(ATTRIB_POSITION);
checkError("glEnableVertexAttribArray - ATTRIB_POSITION");
//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");
//enable vertex attribute
glEnableVertexAttribArray(ATTRIB_COLOR);
checkError("glEnableVertexAttribArray - ATTRIB_COLOR");
// get the window size
int width, height;
glfwGetFramebufferSize(window, &width, &height);
// set viewport
glViewport(0, 0, width, height);
checkError("glViewport");
// define clear for color buffer with black color
glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
checkError("glClearColor");
return GL_TRUE;
}
void gl_rt_window::update()
{
UserData* userData = (UserData*)glfwGetWindowUserPointer(window);
/*
//calculate time delta
double newTime = glfwGetTime();
double timeDelta = newTime - userData->time;
userData->time = newTime;
userData->angleX = fmod(userData->angleX + (X_ROT_SPEED * timeDelta), 2 * M_PI);
//update angle y:
//Fmod = float modulo, [0.0, 2pi]
userData->angleY = fmod(userData->angleY + (Y_ROT_SPEED * timeDelta), 2 * M_PI);
*/
// get the window size
int width, height;
glfwGetFramebufferSize(window, &width, &height);
//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)");
}
void gl_rt_window::draw() {
UserData* userData = (UserData*)glfwGetWindowUserPointer(window);
// clear color buffer
glClear(GL_COLOR_BUFFER_BIT);
// draw stuff
// primitive type, start index in array, number of elements to render
glDrawArrays(GL_TRIANGLES, 0, userData->vertexDataCount);
//TODO
glfwSwapBuffers(window);
}
void gl_rt_window::teardown() {
UserData* userData = (UserData*)glfwGetWindowUserPointer(window);
glDeleteBuffers(1, &(userData->vertexBufferObject));
checkError("glDeleteBuffers");
glDeleteVertexArrays(1, &(userData->vertexArrayObject));
checkError("glDeleteVertexArrays");
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);
}
gl_rt_window::gl_rt_window(GLFWwindow* window)
{
this->window = window;
init();
}
gl_rt_window::~gl_rt_window()
{
}
}
\ No newline at end of file
#include "../include/glCalls.h"
#include <assert.h>
#include <stdlib.h>
#include <assert.h>
void checkError(const char* errorText){
GLenum error = glGetError();
if(error != GL_NO_ERROR){
printf("GLError: %s - %d\n", errorText, error);
exit(0);
}
}
char* readShaderFromFile(char* filename) {
FILE *fp;
long lSize;
char *buffer;
fp = fopen ( filename , "rb" );
assert(fp);
fseek( fp , 0L , SEEK_END);
lSize = ftell( fp );
rewind( fp );
/* allocate memory for entire content */
buffer = calloc( 1, lSize + 1 );
if ( !buffer ) fclose(fp), fputs("memory alloc fails", stderr), exit(1);
/* copy the file into the buffer */
if ( 1 != fread( buffer , lSize, 1 , fp) )
fclose(fp), free(buffer), fputs("entire read fails", stderr), exit(1);
return buffer;
}
GLuint compileShader(GLenum type, const char* shaderSource, char* shaderTag){
//shader handle
GLuint shader;
//create empty shader object
shader = glCreateShader(type);
if(shader == 0) return 0;
//put source code into empty shader object
glShaderSource(shader, 1, &shaderSource, NULL);
//compile shader
glCompileShader(shader);
//compile status
GLint compiled;
//check compile status
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if( !compiled ){
GLint infoLen = 0;
//get length of error message
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if( infoLen > 1){
char* errorMsg = new char[25];
//get error message
glGetShaderInfoLog(shader, infoLen, NULL, errorMsg);
printf("Error compiling shader (%s): %s\n", shaderTag, errorMsg);
delete(errorMsg);
}
// delete shader object
glDeleteShader(shader);
return GL_FALSE;
}
return shader;
}
GLboolean init(GLFWwindow* window){
//allocate memory for user data
UserData* userData