first attempt to bind texture
This commit is contained in:
parent
a00022523f
commit
29fc4f67d4
@ -27,7 +27,9 @@ SET(srcs main.cpp gl/Shader.cpp gl/Shader.h
|
|||||||
blocks/GrasBlock.cpp blocks/GrasBlock.h
|
blocks/GrasBlock.cpp blocks/GrasBlock.h
|
||||||
blocks/BlockRenderer.cpp blocks/BlockRenderer.h
|
blocks/BlockRenderer.cpp blocks/BlockRenderer.h
|
||||||
blocks/RenderBase.cpp blocks/RenderBase.h
|
blocks/RenderBase.cpp blocks/RenderBase.h
|
||||||
gl/Camera.cpp gl/Camera.h)
|
gl/Camera.cpp gl/Camera.h
|
||||||
|
bmploader.cpp bmploader.h
|
||||||
|
gl/Texture.cpp gl/Texture.h)
|
||||||
|
|
||||||
add_executable(opengltest ${srcs})
|
add_executable(opengltest ${srcs})
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include "BlockRenderer.h"
|
#include "BlockRenderer.h"
|
||||||
|
#include "../gl/Texture.h"
|
||||||
|
|
||||||
#define GL_GLEXT_PROTOTYPES
|
#define GL_GLEXT_PROTOTYPES
|
||||||
|
|
||||||
@ -11,15 +12,15 @@
|
|||||||
VertexArray *BlockRenderer::setVertexArray() {
|
VertexArray *BlockRenderer::setVertexArray() {
|
||||||
float cube_vertices[] = {
|
float cube_vertices[] = {
|
||||||
// front
|
// front
|
||||||
-1.0, -1.0, 1.0,
|
-1.0, -1.0, 1.0, 0.0f, 0.0f,
|
||||||
1.0, -1.0, 1.0,
|
1.0, -1.0, 1.0, 1.0f, 0.0f,
|
||||||
1.0, 1.0, 1.0,
|
1.0, 1.0, 1.0, 0.0f, 1.0f,
|
||||||
-1.0, 1.0, 1.0,
|
-1.0, 1.0, 1.0, 1.0f, 1.0f,
|
||||||
// back
|
// back
|
||||||
-1.0, -1.0, -1.0,
|
-1.0, -1.0, -1.0, 0.0f, 0.0f,
|
||||||
1.0, -1.0, -1.0,
|
1.0, -1.0, -1.0, 1.0f, 0.0f,
|
||||||
1.0, 1.0, -1.0,
|
1.0, 1.0, -1.0, 0.0f, 1.0f,
|
||||||
-1.0, 1.0, -1.0
|
-1.0, 1.0, -1.0, 1.0f, 1.0f,
|
||||||
};
|
};
|
||||||
|
|
||||||
GLfloat cube_colors[] = {
|
GLfloat cube_colors[] = {
|
||||||
@ -83,6 +84,12 @@ Shader BlockRenderer::setShader() {
|
|||||||
Shader s;
|
Shader s;
|
||||||
s.loadShader(vertsrc, geosrc, fragsrc);
|
s.loadShader(vertsrc, geosrc, fragsrc);
|
||||||
s.Bind();
|
s.Bind();
|
||||||
|
|
||||||
|
Texture t;
|
||||||
|
t.Bind();
|
||||||
|
glUniform1i(s.getUniformHandle("u_texture"), 0);
|
||||||
|
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
R"(#version 330 core
|
R"(#version 330 core
|
||||||
out vec4 color;
|
out vec4 color;
|
||||||
uniform vec3 u_color;
|
uniform vec3 u_color;
|
||||||
|
uniform sampler2D u_texture;
|
||||||
|
|
||||||
in vec3 normal;
|
in vec3 normal;
|
||||||
in vec4 pos;
|
in vec4 pos;
|
||||||
|
in vec2 v_texcoords;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
@ -15,6 +17,9 @@ void main()
|
|||||||
// set light color
|
// set light color
|
||||||
vec3 diffuse = diff * vec3(1.0,1.0,1.0);
|
vec3 diffuse = diff * vec3(1.0,1.0,1.0);
|
||||||
|
|
||||||
vec3 result = (diffuse) * u_color;
|
vec4 c = texture(u_texture, v_texcoords);
|
||||||
color = vec4(result,0.0);
|
|
||||||
|
vec3 result = (diffuse) * c.xyz;
|
||||||
|
// color = vec4(result,0.0);
|
||||||
|
color = c;
|
||||||
})";
|
})";
|
@ -4,7 +4,6 @@ layout(triangles) in;
|
|||||||
layout(triangle_strip, max_vertices=3) out;
|
layout(triangle_strip, max_vertices=3) out;
|
||||||
|
|
||||||
out vec3 normal;
|
out vec3 normal;
|
||||||
|
|
||||||
out vec4 pos;
|
out vec4 pos;
|
||||||
|
|
||||||
void main( void )
|
void main( void )
|
||||||
|
@ -1,16 +1,17 @@
|
|||||||
R"(#version 330 core
|
R"(#version 330 core
|
||||||
// Input vertex data, different for all executions of this shader.
|
// 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 vec2 texCoords;
|
||||||
|
|
||||||
//// Values that stay constant for the whole mesh.
|
//// Values that stay constant for the whole mesh.
|
||||||
uniform mat4 MVP;
|
uniform mat4 MVP;
|
||||||
uniform mat4 translation;
|
uniform mat4 translation;
|
||||||
|
|
||||||
out vec3 FragPos;
|
out vec2 v_texcoords;
|
||||||
|
|
||||||
void main(){
|
void main(){
|
||||||
// Output position of the vertex, in clip space : MVP * position
|
// Output position of the vertex, in clip space : MVP * position
|
||||||
vec4 pos = MVP * translation * vec4(vertexPosition_modelspace,1);
|
vec4 pos = MVP * translation * vec4(vertexPosition_modelspace,1);
|
||||||
gl_Position = pos;
|
gl_Position = pos;
|
||||||
FragPos = pos.xyz;
|
v_texcoords = texCoords;
|
||||||
})";
|
})";
|
73
bmploader.cpp
Normal file
73
bmploader.cpp
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
//
|
||||||
|
// Created by lukas on 05.02.22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <GL/gl.h>
|
||||||
|
#include "bmploader.h"
|
||||||
|
|
||||||
|
unsigned bmploader::loadBMP(const char *imagepath) {
|
||||||
|
// Data read from the header of the BMP file
|
||||||
|
unsigned char header[54]; // Each BMP file begins by a 54-bytes header
|
||||||
|
unsigned int dataPos; // Position in the file where the actual data begins
|
||||||
|
int width, height;
|
||||||
|
unsigned int imageSize; // = width*height*3
|
||||||
|
// Actual RGB data
|
||||||
|
unsigned char * data;
|
||||||
|
|
||||||
|
|
||||||
|
// Open the file
|
||||||
|
FILE * file = fopen(imagepath,"rb");
|
||||||
|
if (!file){printf("Image could not be opened\n"); return 0;}
|
||||||
|
|
||||||
|
if ( fread(header, 1, 54, file)!=54 ){ // If not 54 bytes read : problem
|
||||||
|
printf("Not a correct BMP file\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( header[0]!='B' || header[1]!='M' ){
|
||||||
|
printf("Not a correct BMP file\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read ints from the byte array
|
||||||
|
dataPos = *(int*)&(header[0x0A]);
|
||||||
|
imageSize = *(int*)&(header[0x22]);
|
||||||
|
width = *(int*)&(header[0x12]);
|
||||||
|
height = *(int*)&(header[0x16]);
|
||||||
|
|
||||||
|
// Some BMP files are misformatted, guess missing information
|
||||||
|
if (imageSize==0) imageSize=width*height*3; // 3 : one byte for each Red, Green and Blue component
|
||||||
|
if (dataPos==0) dataPos=54; // The BMP header is done that way
|
||||||
|
|
||||||
|
// Create a buffer
|
||||||
|
data = new unsigned char [imageSize];
|
||||||
|
|
||||||
|
// Read the actual data from the file into the buffer
|
||||||
|
fread(data,1,imageSize,file);
|
||||||
|
|
||||||
|
//Everything is in memory now, the file can be closed
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
|
||||||
|
// Create one OpenGL texture
|
||||||
|
unsigned textureID;
|
||||||
|
glGenTextures(1, &textureID);
|
||||||
|
|
||||||
|
// "Bind" the newly created texture : all future texture functions will modify this texture
|
||||||
|
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||||
|
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
|
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||||
|
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||||
|
|
||||||
|
// Give the image to OpenGL
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return textureID;
|
||||||
|
}
|
15
bmploader.h
Normal file
15
bmploader.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
//
|
||||||
|
// Created by lukas on 05.02.22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef OPENGLTEST_BMPLOADER_H
|
||||||
|
#define OPENGLTEST_BMPLOADER_H
|
||||||
|
|
||||||
|
|
||||||
|
class bmploader {
|
||||||
|
public:
|
||||||
|
unsigned loadBMP(const char * imagepath);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //OPENGLTEST_BMPLOADER_H
|
22
gl/Texture.cpp
Normal file
22
gl/Texture.cpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
//
|
||||||
|
// Created by lukas on 06.02.22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Texture.h"
|
||||||
|
#include "../bmploader.h"
|
||||||
|
#define GL_GLEXT_PROTOTYPES
|
||||||
|
|
||||||
|
#include <GL/gl.h>
|
||||||
|
|
||||||
|
Texture::Texture() {
|
||||||
|
mTexturehandle = bmploader().loadBMP("../block.bmp");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Texture::Bind() {
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, mTexturehandle);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Texture::UnBind() {
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
}
|
19
gl/Texture.h
Normal file
19
gl/Texture.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
//
|
||||||
|
// Created by lukas on 06.02.22.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef OPENGLTEST_TEXTURE_H
|
||||||
|
#define OPENGLTEST_TEXTURE_H
|
||||||
|
|
||||||
|
|
||||||
|
class Texture {
|
||||||
|
private:
|
||||||
|
unsigned mTexturehandle;
|
||||||
|
public:
|
||||||
|
Texture();
|
||||||
|
void Bind();
|
||||||
|
void UnBind();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //OPENGLTEST_TEXTURE_H
|
@ -13,12 +13,14 @@ VertexArray::VertexArray(const VertexBuffer& buff) {
|
|||||||
buff.Bind();
|
buff.Bind();
|
||||||
|
|
||||||
// generate new vertex array object
|
// generate new vertex array object
|
||||||
glGenVertexArrays(1, &handle);
|
glGenVertexArrays(2, &handle);
|
||||||
Bind();
|
Bind();
|
||||||
|
|
||||||
// specify syntax of my data
|
// specify syntax of my data
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) nullptr);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) nullptr);
|
||||||
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) nullptr);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned VertexArray::getHandle() const {
|
unsigned VertexArray::getHandle() const {
|
||||||
|
Loading…
Reference in New Issue
Block a user