168 lines
4.4 KiB
C++
168 lines
4.4 KiB
C++
#include <iostream>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <chrono>
|
|
#define GLEW_STATIC
|
|
#include <GL/glew.h>
|
|
#include <SDL2/SDL.h>
|
|
#include <SDL2/SDL_opengl.h>
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
// init
|
|
SDL_Init(SDL_INIT_VIDEO);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
|
|
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
|
|
|
SDL_Window* window = SDL_CreateWindow("OpenGL", 100, 100, 800, 600, SDL_WINDOW_OPENGL);
|
|
|
|
SDL_GLContext context = SDL_GL_CreateContext(window);
|
|
|
|
glewExperimental = GL_TRUE;
|
|
glewInit();
|
|
|
|
// shader laden
|
|
|
|
std::ifstream frag ("shaders/fragment/simple.glsl");
|
|
std::string fragcodes;
|
|
|
|
if(frag.is_open()){
|
|
std::ostringstream ss;
|
|
ss << frag.rdbuf();
|
|
fragcodes = ss.str();
|
|
}else{
|
|
std::cout << "hää? frag shader existiert nicht!";
|
|
}
|
|
|
|
std::ifstream vert ("shaders/vertex/simple.glsl");
|
|
std::string vertcodes;
|
|
|
|
if(vert.is_open()){
|
|
std::ostringstream ss;
|
|
ss << vert.rdbuf();
|
|
vertcodes = ss.str();
|
|
}else{
|
|
std::cout << "hää? frag shader existiert nicht!";
|
|
}
|
|
|
|
char* vertcode = new char[vertcodes.length() + 1];
|
|
strcpy(vertcode, vertcodes.c_str());
|
|
char* fragcode = new char[fragcodes.length() + 1];
|
|
strcpy(fragcode, fragcodes.c_str());
|
|
|
|
|
|
|
|
// dreieck
|
|
|
|
GLfloat vertices[] = {
|
|
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // Top-left
|
|
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // Top-right
|
|
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // Bottom-right
|
|
-0.5f, -0.5f, 1.0f, 1.0f, 1.0f // Bottom-left
|
|
};
|
|
|
|
GLuint elements[] = {
|
|
0, 2, 3,
|
|
1, 2, 3
|
|
};
|
|
|
|
|
|
//shaders
|
|
|
|
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
|
glShaderSource(vertexShader, 1, &vertcode, NULL);
|
|
glCompileShader(vertexShader);
|
|
|
|
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
|
glShaderSource(fragmentShader, 1, &fragcode, NULL);
|
|
glCompileShader(fragmentShader);
|
|
|
|
|
|
GLuint shaderProgram = glCreateProgram();
|
|
glAttachShader(shaderProgram, vertexShader);
|
|
glAttachShader(shaderProgram, fragmentShader);
|
|
glLinkProgram(shaderProgram);
|
|
|
|
GLuint vao;
|
|
glGenVertexArrays(1, &vao);
|
|
|
|
//vbo
|
|
GLuint vbo;
|
|
glGenBuffers(1, &vbo); // Generate 1 buffer
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
|
|
//ebo
|
|
GLuint ebo;
|
|
glGenBuffers(1, &ebo); // Generate 1 buffer
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
|
|
|
|
|
|
//-----vao-----
|
|
|
|
glBindVertexArray(vao);
|
|
//-----vao-----
|
|
|
|
glUseProgram(shaderProgram);
|
|
|
|
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
|
|
glEnableVertexAttribArray(posAttrib);
|
|
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), 0);
|
|
|
|
GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
|
|
glEnableVertexAttribArray(colAttrib);
|
|
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
|
|
|
|
//glBindFragDataLocation(shaderProgram, 0, "outColor");
|
|
|
|
//GLint uniColor = glGetUniformLocation(shaderProgram, "triangleColor");
|
|
//glUniform3f(uniColor, 1.0f, 0.0f, 0.0f);
|
|
|
|
|
|
|
|
// loopdeloop
|
|
|
|
auto t_start = std::chrono::high_resolution_clock::now();
|
|
SDL_Event windowEvent;
|
|
while (true)
|
|
{
|
|
//auto t_now = std::chrono::high_resolution_clock::now();
|
|
//float time = std::chrono::duration_cast<std::chrono::duration<float>>(t_now - t_start).count();
|
|
//glUniform3f(uniColor, (sin(time * 4.0f) + 1.0f) / 2.0f, 0.0f, 0.0f);
|
|
if (SDL_PollEvent(&windowEvent))
|
|
{
|
|
if (windowEvent.type == SDL_QUIT) break;
|
|
if (windowEvent.type == SDL_KEYUP &&
|
|
windowEvent.key.keysym.sym == SDLK_ESCAPE) break;
|
|
}
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, elements);
|
|
|
|
SDL_GL_SwapWindow(window);
|
|
}
|
|
|
|
|
|
// bier bier
|
|
|
|
glDeleteProgram(shaderProgram);
|
|
glDeleteShader(fragmentShader);
|
|
glDeleteShader(vertexShader);
|
|
|
|
glDeleteBuffers(1, &vbo);
|
|
|
|
//glDeleteVertexArrays(1, &vao);
|
|
|
|
SDL_GL_DeleteContext(context);
|
|
|
|
SDL_Quit();
|
|
return 0;
|
|
}
|