Coloring triangles with texture

This commit is contained in:
Caesar2011
2016-09-05 19:45:14 +02:00
parent 433983e4ff
commit aed54bd694
8 changed files with 37 additions and 22 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 768 KiB

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

After

Width:  |  Height:  |  Size: 108 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 876 KiB

After

Width:  |  Height:  |  Size: 106 KiB

View File

@@ -44,8 +44,8 @@ GLuint LoadTexture(char *filename,int *textw,int *texth) {
// this reads from the sdl surface and puts it into an opengl texture // this reads from the sdl surface and puts it into an opengl texture
glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode, GL_UNSIGNED_BYTE, surface->pixels); glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode, GL_UNSIGNED_BYTE, surface->pixels);
// these affect how this texture is drawn later on...glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // these affect how this texture is drawn later on...glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// clean up // clean up
@@ -223,13 +223,13 @@ int main(int argc, char* args[]) {
-1.0f, 1.0f, 0.0f, // (top - left ) -1.0f, 1.0f, 0.0f, // (top - left )
}; };
// One color for each vertex. They were generated randomly. // One color for each vertex. They were generated randomly.
static const GLfloat g_color_buffer_data[] = { static const GLfloat g_uv_buffer_data[] = {
0.583f, 0.771f, 0.014f, 0.0f, 1.0f-0.0f, // (bottom - left ) TRIANGLE 1
0.609f, 0.115f, 0.436f, 1.0f, 1.0f-0.0f, // (bottom - right)
0.327f, 0.483f, 0.844f, 0.0f, 1.0f-1.0f, // (top - left )
0.822f, 0.569f, 0.201f, 1.0f, 1.0f-1.0f, // (top - right) TRIANGLE 2
0.435f, 0.602f, 0.223f, 1.0f, 1.0f-0.0f, // (bottom - right)
0.310f, 0.747f, 0.185f, 0.0f, 1.0f-1.0f, // (top - left )
}; };
@@ -243,7 +243,7 @@ int main(int argc, char* args[]) {
GLuint colorbuffer; GLuint colorbuffer;
glGenBuffers(1, &colorbuffer); glGenBuffers(1, &colorbuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);
@@ -251,12 +251,12 @@ int main(int argc, char* args[]) {
tex_jpe=LoadTexture("../img/tests/test.jpeg",&textw,&texth); tex_jpe=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, "myTextureSampler"); GLint textureLocation = glGetUniformLocationARB(programID, "myTextureSampler");
//glUniform1iARB(textureLocation, tex_jpe); glUniform1i(textureLocation, tex_jpe);
@@ -287,7 +287,7 @@ int main(int argc, char* args[]) {
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glVertexAttribPointer( glVertexAttribPointer(
1, // attribute. No particular reason for 1, but must match the layout in the shader. 1, // attribute. No particular reason for 1, but must match the layout in the shader.
3, // size 2, // size
GL_FLOAT, // type GL_FLOAT, // type
GL_FALSE, // normalized? GL_FALSE, // normalized?
0, // stride 0, // stride

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>
1473093222 source:d:\owncloud\documents\programmierung\cpp\imanox-chroma-matting\opengl-test-two\main.cpp 1473097371 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,8 +1,17 @@
#version 330 core #version 330 core
in vec3 fragmentColor; // Interpolated values from the vertex shaders
in vec2 UV;
// Ouput data
out vec3 color; out vec3 color;
// Values that stay constant for the whole mesh.
uniform sampler2D myTextureSampler;
void main(){ void main(){
color = fragmentColor;
// Output color = color of the texture at the specified UV
color = texture( myTextureSampler, UV ).rgb;
} }

View File

@@ -1,11 +1,17 @@
#version 330 core #version 330 core
// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace; layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec3 vertexColor; layout(location = 1) in vec2 vertexUV;
out vec3 fragmentColor;
// Output data ; will be interpolated for each fragment.
out vec2 UV;
void main(){ void main(){
gl_Position.xyz = vertexPosition_modelspace; // Output position of the vertex, in clip space : MVP * position
gl_Position.w = 1.0; gl_Position = vec4(vertexPosition_modelspace,1);
fragmentColor = vertexColor;
// UV of the vertex. No special space for this one.
UV = vertexUV;
} }