Commit 45eba40b authored by Brian's avatar Brian

deleted objimport, added width and height in userdata

parent d1028440
......@@ -152,12 +152,14 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\src\glad.c" />
<ClCompile Include="..\..\src\glCalls.c" />
<ClCompile Include="mm_raytracer.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\glad\glad.h" />
<ClInclude Include="..\..\include\glCalls.h" />
<ClInclude Include="..\..\include\KHR\khrplatform.h" />
<ClInclude Include="sphere.h" />
<ClInclude Include="..\..\include\sphere.h" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
......
......@@ -27,17 +27,23 @@
<ClCompile Include="..\..\src\glad.c">
<Filter>Quelldateien\src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\glCalls.c">
<Filter>Quelldateien\src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="sphere.h">
<Filter>Headerdateien\include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\glad\glad.h">
<Filter>Headerdateien\include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\KHR\khrplatform.h">
<Filter>Headerdateien\include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\sphere.h">
<Filter>Headerdateien\include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\glCalls.h">
<Filter>Headerdateien\include</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
......
#ifndef GL_CALLS_H
#define GL_CALLS_H
#ifndef GLCALLS_H
#define GLCALLS_H
typedef struct
{
float x;
float y;
float z;
} Vec3;
#include "../include/glad/glad.h"
#include <GLFW/glfw3.h>
#include <math.h>
typedef struct
{
Vec3 position;
float radius;
} Sphere;
#define ATTRIB_POSITION 0
#define ATTRIB_COLOR 1
#define Y_ROT_SPEED 10.0
#define X_ROT_SPEED 10.0
typedef struct{
GLuint programObject;
GLuint vertexBufferObject;
GLuint vertexArrayObject;
GLuint vertexDataCount;
//todo
double time;
GLfloat angleY;
GLint angleYLoc;
GLfloat angleX;
GLint angleXLoc;
GLint height;
GLint width;
} UserData;
typedef struct
{
Vec3 position;
Vec3 direction;
} Ray;
GLfloat position[3];
GLubyte color[3];
} VertexData;
#endif
\ No newline at end of file
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 "../include/glCalls.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.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 = malloc(sizeof(char) * infoLen);
//get error message
glGetShaderInfoLog(shader, infoLen, NULL, errorMsg);
printf("Error compiling shader (%s): %s\n", shaderTag, errorMsg);
free(errorMsg);
}
// delete shader object
glDeleteShader(shader);
return GL_FALSE;
}
return shader;
}
GLboolean init(GLFWwindow* window){
//allocate memory for user data
UserData* userData = malloc(sizeof(UserData));
glfwSetWindowUserPointer(window, (void*)userData);
//Create the vertex shader:
char* vertexShaderSource = readShaderFromFile("shader/vertex.glsl");
GLuint vertexShader = compileShader(GL_VERTEX_SHADER, vertexShaderSource, "Vertex shader");
free(vertexShaderSource);
//Create the fragment shader:
char* fragmentShaderSource = readShaderFromFile("shader/fragment.glsl");
GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource, "Fragment shader");
free(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 = malloc(sizeof(char) * infoLen);
//get error message from program object
glGetProgramInfoLog(programObject, infoLen, NULL, errorMsg);
printf("Error linking program: %s\n", errorMsg);
free(errorMsg);
}
glDeleteProgram(programObject);
return GL_FALSE;
}
// use program
glUseProgram(programObject);
checkError("glUseProgram");
// store program object in context
userData->programObject = programObject;
//get uniform locations
userData->angleXLoc = glGetUniformLocation(userData->programObject, "angle_x");
checkError("glGetUniformLocation (angle_x)");
assert(userData->angleYLoc >= 0);
userData->angleYLoc = glGetUniformLocation(userData->programObject, "angle_y");
checkError("glGetUniformLocation (angle_y)");
assert(userData->angleYLoc >= 0);
//initialize the modell
userData->time = glfwGetTime();
userData->angleY = 0.0f;
userData->angleX = 0.0f;
// 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");
//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 update(GLFWwindow* window)
{
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
glUniform1f(userData->angleYLoc, userData->angleY);
checkError("glUniform1f (angleY)");
}
void draw (GLFWwindow* window){
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 teardown(GLFWwindow* window){
UserData* userData = (UserData*) glfwGetWindowUserPointer(window);
glDeleteBuffers(1, &(userData->vertexBufferObject));
checkError("glDeleteBuffers");
glDeleteVertexArrays(1, &(userData->vertexArrayObject));
checkError("glDeleteVertexArrays");
free(userData);
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
}
void error_callback(int error, const char* description){
fprintf(stderr, "Error: %s\n", description);
}
\ 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