Merge branch 'main' of github.com:Caenneth/cg1_purple

This commit is contained in:
Luca Conte 2024-06-24 13:08:55 +02:00
commit 1ec82871ad
2 changed files with 21 additions and 2 deletions

View File

@ -3,14 +3,16 @@
#include <GL/glew.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
/**
* Loads an object.
* Basically generates vbo and vao for an object and saving them in the ObjectData struct.
*/
void load_object(ObjectData* objectData) {
// write faces to buffer
//GLuint triangleVertexBufferObject;
assert(objectData != NULL);
// write faces to buffer
glGenBuffers(1, &objectData->vbo);
glBindBuffer(GL_ARRAY_BUFFER, objectData->vbo);
glBufferData(GL_ARRAY_BUFFER, objectData->object->length * sizeof(face), objectData->object->faces, GL_STATIC_DRAW);
@ -75,6 +77,10 @@ void load_object(ObjectData* objectData) {
* Returns an array of objects.
*/
ObjectData* readObjFiles(char** path, int numModels, int* count) {
assert(path != NULL);
assert(count != NULL);
assert(numModels > 0);
ObjectData* objects = (ObjectData*) malloc(sizeof(ObjectData) * numModels);
*count += numModels;
if (!objects) {
@ -95,6 +101,8 @@ ObjectData* readObjFiles(char** path, int numModels, int* count) {
* Returns an array of objects.
*/
ObjectData* readSingleObjFile(char* path) {
assert(path != NULL);
ObjectData* objectData = (ObjectData*) malloc(sizeof(ObjectData));
if (!objectData) {
@ -114,6 +122,8 @@ ObjectData* readSingleObjFile(char* path) {
* Used to clean up the draw calls in Main().
*/
void draw_object(ObjectData* objectData) {
assert(objectData != NULL);
glBindVertexArray(objectData->vao);
glDrawArrays(GL_TRIANGLES, 0, objectData->object->length * 3); // Annahme: Jedes face hat 3 vertices
glBindVertexArray(0);

View File

@ -2,8 +2,12 @@
#include <stdlib.h>
#include <GL/glew.h>
#include "shader.h"
#include <assert.h>
void compileShader(const GLchar *shaderSource, Shader *shader) {
assert(shaderSource != NULL);
assert(shader != NULL);
shader->id = glCreateShader(shader->type);
glShaderSource(shader->id, 1, &shaderSource, NULL);
@ -23,12 +27,17 @@ void compileShader(const GLchar *shaderSource, Shader *shader) {
}
void compileShaders(Shader* shaders, const GLchar** glslSources, int numShaders) {
assert(glslSources != NULL);
assert(shaders != NULL);
for (int i = 0; i < numShaders; i++) {
compileShader(glslSources[i], &shaders[i]);
}
}
ShaderProgram createShaderProgram(const GLchar* vertexSource, const GLchar* fragmentSource) {
assert(vertexSource != NULL);
assert(fragmentSource != NULL);
Shader vertexShader = { GL_VERTEX_SHADER, 0 };
Shader fragmentShader = { GL_FRAGMENT_SHADER, 0 };