Files
cpp-imanox-chroma-matting/opengl-test-two/main.cpp
2016-09-09 11:55:56 +02:00

170 lines
5.4 KiB
C++

// http://stackoverflow.com/questions/6005076/building-glew-on-windows-with-mingw
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <SDL2/SDL.h>
#include <fstream>
#include <stdexcept>
#include "loaders.h"
int main(int argc, char* args[]) {
GLFWwindow* window;
GLuint tex1, tex2;
int textw, texth;
int textw2, texth2;
/* Initialize the library */
/* GLFW */
if (!glfwInit()) {
printf("GLFW Error");
return 1;
}
/* Create a windowed mode window and its OpenGL context */
glfwSetWindowSizeLimits(window, 640, 480, 640, 480);
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window) {
glfwTerminate();
return 1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* GLEW */
glewExperimental = GL_FALSE;
GLenum error = glGetError();
if (error != GL_NO_ERROR)
{
printf("OpenGL Error: %d",error);
}
GLenum glewinit = glewInit();
if (glewinit != GLEW_OK) {
printf("Glew not okay! %d",glewinit);
return 1;
}
/* SDL */
if (InitLoaders() < 0) {
printf("SDL Error");
return 1;
}
// INIT
// An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f, // (bottom - left ) TRIANGLE 1
1.0f, -1.0f, 0.0f, // (bottom - right)
-1.0f, 1.0f, 0.0f, // (top - left )
1.0f, 1.0f, 0.0f, // (top - right) TRIANGLE 2
1.0f, -1.0f, 0.0f, // (bottom - right)
-1.0f, 1.0f, 0.0f, // (top - left )
};
// One color for each vertex. They were generated randomly.
static const GLfloat g_uv_buffer_data[] = {
0.0f, 1.0f-0.0f, // (bottom - left ) TRIANGLE 1
1.0f, 1.0f-0.0f, // (bottom - right)
0.0f, 1.0f-1.0f, // (top - left )
1.0f, 1.0f-1.0f, // (top - right) TRIANGLE 2
1.0f, 1.0f-0.0f, // (bottom - right)
0.0f, 1.0f-1.0f, // (top - left )
};
// Identify our vertex buffer
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
// Identify our color buffer
GLuint colorbuffer;
glGenBuffers(1, &colorbuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);
tex1 = LoadTexture("../img/tests/flieger.jpg",&textw,&texth);
printf("Tex1: %d", tex1);
tex2 = LoadTexture("../img/tests/flieger2.jpg",&textw2,&texth2);
printf("Tex2: %d", tex2);
glfwSetWindowSize(window, textw, texth);
glViewport(0, 0, textw, texth);
glfwSetWindowSizeLimits(window, textw, texth, textw, texth);
GLuint programID = LoadShaders("trimap.vertexshader", "trimap.fragmentshader");
GLint textureLoc1 = glGetUniformLocation(programID, "myTextureSampler");
GLint textureLoc2 = glGetUniformLocation(programID, "myTextureSampler2");
GLint widthLoc = glGetUniformLocation(programID, "pixWidth");
GLint heigthLoc = glGetUniformLocation(programID, "pixHeight");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, tex2);
printf("Shader program ID: %d\n", programID);
printf("textureLoc1: %d\n", textureLoc1);
printf("textureLoc2: %d\n", textureLoc2);
printf("widthLoc: %d\n", widthLoc);
printf("heigthLoc: %d\n", heigthLoc);
printf("Width: %f\n", 1.0f/textw);
printf("Height: %f\n", 1.0f/texth);
// INIT END
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window)) {
/* Render here */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
glUniform1f(widthLoc, 1.0f/textw);
glUniform1f(heigthLoc, 1.0f/texth);
glUniform1i(textureLoc1, 0);
glUniform1i(textureLoc2, 1);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// 2nd attribute buffer : colors
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glVertexAttribPointer(
1, // attribute. No particular reason for 1, but must match the layout in the shader.
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 2*3); // 2*3 indices starting at 0 -> 2 triangles -> 1 square
glDisableVertexAttribArray(0);
// RENDER END
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}