Colored triangles with VAO and VBO

This commit is contained in:
Caesar2011
2016-09-05 18:38:53 +02:00
parent 866fabdf45
commit 433983e4ff
5 changed files with 87 additions and 41 deletions

View File

@@ -169,10 +169,6 @@ GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path
int main(int argc, char* args[]) { int main(int argc, char* args[]) {
GLFWwindow* window; GLFWwindow* window;
GLuint tex_jpe; GLuint tex_jpe;
GLuint tex_jpg;
GLuint tex_gif;
GLuint tex_png;
GLuint tex_bmp;
int textw, texth; int textw, texth;
int timer = 0; int timer = 0;
@@ -213,15 +209,56 @@ int main(int argc, char* args[]) {
} }
// INIT // INIT
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
// 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_color_buffer_data[] = {
0.583f, 0.771f, 0.014f,
0.609f, 0.115f, 0.436f,
0.327f, 0.483f, 0.844f,
0.822f, 0.569f, 0.201f,
0.435f, 0.602f, 0.223f,
0.310f, 0.747f, 0.185f,
};
// 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_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);
tex_jpe=LoadTexture("../img/tests/test.jpeg",&textw,&texth); tex_jpe=LoadTexture("../img/tests/test.jpeg",&textw,&texth);
//tex_jpg=LoadTexture("../img/tests/test.jpg",&textw,&texth);
//tex_png=LoadTexture("../img/tests/test.png",&textw,&texth);
//tex_bmp=LoadTexture("../img/tests/test.bmp",&textw,&texth);
GLuint programID = LoadShaders("trimap.vertexshader", "trimap.fragmentshader"); GLuint programID = LoadShaders("trimap.vertexshader", "trimap.fragmentshader");
printf("%d", programID); printf("%d", programID);
GLint textureLocation = glGetUniformLocationARB(programID, "texture"); //GLint textureLocation = glGetUniformLocationARB(programID, "myTextureSampler");
glUniform1iARB(textureLocation, tex_jpe); //glUniform1iARB(textureLocation, tex_jpe);
// INIT END // INIT END
/* Loop until the user closes the window */ /* Loop until the user closes the window */
@@ -234,21 +271,35 @@ int main(int argc, char* args[]) {
glUseProgram(programID); glUseProgram(programID);
// RENDER // 1rst attribute buffer : vertices
/*if (timer<200) glEnableVertexAttribArray(0);
DrawTexture(50,50,tex_jpe,textw,texth); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
else if (timer<400) glVertexAttribPointer(
DrawTexture(50,50,tex_jpg,textw,texth); 0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
else if (timer<600) 3, // size
DrawTexture(50,50,tex_png,textw,texth); GL_FLOAT, // type
else GL_FALSE, // normalized?
DrawTexture(50,50,tex_bmp,textw,texth);*/ 0, // stride
glBegin(GL_QUADS); (void*)0 // array buffer offset
glTexCoord2d(0.0,0.0); glVertex2f(-1, -1); );
glTexCoord2d(1.0,0.0); glVertex2f(1, -1); // 2nd attribute buffer : colors
glTexCoord2d(1.0,1.0); glVertex2f(1, 0); glEnableVertexAttribArray(1);
glTexCoord2d(0.0,1.0); glVertex2f(-1, 0); glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glEnd(); glVertexAttribPointer(
1, // attribute. No particular reason for 1, but must match the layout in the shader.
3, // 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 // RENDER END
/* Swap front and back buffers */ /* Swap front and back buffers */

View File

@@ -2,7 +2,7 @@
1385320478 source:d:\owncloud\documents\programmierung\cpp\opengl-test-two\main.cpp 1385320478 source:d:\owncloud\documents\programmierung\cpp\opengl-test-two\main.cpp
<iostream> <iostream>
1473070070 source:d:\owncloud\documents\programmierung\cpp\imanox-chroma-matting\opengl-test-two\main.cpp 1473093222 source:d:\owncloud\documents\programmierung\cpp\imanox-chroma-matting\opengl-test-two\main.cpp
<GL/glew.h> <GL/glew.h>
<GLFW/glfw3.h> <GLFW/glfw3.h>
<SDL2/SDL.h> <SDL2/SDL.h>

View File

@@ -1,11 +1,8 @@
#version 330 core #version 330 core
uniform sampler2D texture; in vec3 fragmentColor;
in vec2 vTexCoord; out vec3 color;
out vec4 color;
void main(){ void main(){
//color = vec4(1f,0f,0f,1.0f); color = fragmentColor;
vec2 tcoord=vec2(0.5f,0.5f);
color = vec4(texelFetch(texture, ivec2(vTexCoord), 0).rgb, 1);
} }

View File

@@ -1,13 +1,11 @@
#version 400 #version 330 core
in vec2 aVertex; layout(location = 0) in vec3 vertexPosition_modelspace;
in vec2 aTexCoord; layout(location = 1) in vec3 vertexColor;
out vec3 fragmentColor;
out vec2 vTexCoord; void main(){
out vec3 vNormal; gl_Position.xyz = vertexPosition_modelspace;
gl_Position.w = 1.0;
void main() fragmentColor = vertexColor;
{
vTexCoord = aTexCoord;
gl_Position = vec3(aVertex, 1);
} }