From 29fc4f67d4a2e7632db62f55cb466b39a5a8830e Mon Sep 17 00:00:00 2001 From: lukas Date: Sun, 6 Feb 2022 14:47:33 +0100 Subject: [PATCH 1/4] first attempt to bind texture --- CMakeLists.txt | 4 ++- block.bmp | Bin 0 -> 12342 bytes blocks/BlockRenderer.cpp | 23 +++++++----- blocks/fragment.shader | 9 +++-- blocks/geometry.shader | 1 - blocks/vertex.shader | 5 +-- bmploader.cpp | 73 +++++++++++++++++++++++++++++++++++++++ bmploader.h | 15 ++++++++ gl/Texture.cpp | 22 ++++++++++++ gl/Texture.h | 19 ++++++++++ gl/VertexArray.cpp | 6 ++-- 11 files changed, 161 insertions(+), 16 deletions(-) create mode 100644 block.bmp create mode 100644 bmploader.cpp create mode 100644 bmploader.h create mode 100644 gl/Texture.cpp create mode 100644 gl/Texture.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ffb030d..d0a5966 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,9 @@ SET(srcs main.cpp gl/Shader.cpp gl/Shader.h blocks/GrasBlock.cpp blocks/GrasBlock.h blocks/BlockRenderer.cpp blocks/BlockRenderer.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}) diff --git a/block.bmp b/block.bmp new file mode 100644 index 0000000000000000000000000000000000000000..e3d63663bf4fe835441663871eb2fa2a242aa73c GIT binary patch literal 12342 zcmcJPJ8m6U5Jc%+I9KAxaHQB!DtIBvy??l&HjfI`pSd%9d;o>I)u*caL889>^YyQ{ zpP#Ss=O6L&_y0e?z5O04J~99DXa1z|_V(}b=llETIPnX7kADAUovFvcTK|!%$610Q z>Pfd8s&1tcCTrrdO8HB82dLVyc1pI2mRFOAnh*5j9)gqDk0G^Vms5I7+zJM4@q8{N z@C(z9e!PEjO3yB86}*lx5js_U2`H@k{rn$k(sLr{Q5X}kPi-%$LowY?T(?&pn3)7s zD5E|a0Oy&w>;Y)Sv{d1i0l1u=lWkR+l&Dzv80{p}wuEUbGy2-+J?>IKJ|#0iKD=Dg{Dwf2`BdzTHTB?%92MQl-p; z`!ycgzIZzOk^7YX1%t75_;<4et#Ij?MpR8e_%OC^@~KiF{_gOXPGwFsD|Q$x1Y~Qr z(;#1XfBVmvy{(u|RsZgVWZR94+1^hZA~AaKaYA5*(EHU_UiHMJucYb2`{~a(&%465 z^VFVd;w(ZNb{!=d^sV-Gs{f}#HHocS^S)ZD%({z*xJz?fsY7bU;=h>w} z%qkSiX8rZ4d^(oOU<(`BGS$>notk6t$^F@2L930Y0;wxg9ZOZ9Iz(c0et*COLu4&+ z9WiC~G@X2ctsvD-)8sH^2&RY;n2t1(TBnoFs+z414RwUWVK^zsh6pDyGc6KN1zJ6= zt5s8*kyD{=&xksqt;e1Q^PJXMyxmAiiZ-IA@%mJ3*P05w!#cI#^wfyy2N7$?r}eZ< zn*8mw5i@x%g)oQ(UPi=IOl01F2^(*v5Q|csNombr- z{k4j9_1fwHn^00m>DQ$@uhW<7dR|>|!jQTJlX@&p)}1hV0KA%?r&$+b+H%5>1;+VQ t!Mc~{sXTQzNi2I literal 0 HcmV?d00001 diff --git a/blocks/BlockRenderer.cpp b/blocks/BlockRenderer.cpp index 576a808..ef71562 100644 --- a/blocks/BlockRenderer.cpp +++ b/blocks/BlockRenderer.cpp @@ -3,6 +3,7 @@ // #include "BlockRenderer.h" +#include "../gl/Texture.h" #define GL_GLEXT_PROTOTYPES @@ -11,15 +12,15 @@ VertexArray *BlockRenderer::setVertexArray() { float cube_vertices[] = { // front - -1.0, -1.0, 1.0, - 1.0, -1.0, 1.0, - 1.0, 1.0, 1.0, - -1.0, 1.0, 1.0, + -1.0, -1.0, 1.0, 0.0f, 0.0f, + 1.0, -1.0, 1.0, 1.0f, 0.0f, + 1.0, 1.0, 1.0, 0.0f, 1.0f, + -1.0, 1.0, 1.0, 1.0f, 1.0f, // back - -1.0, -1.0, -1.0, - 1.0, -1.0, -1.0, - 1.0, 1.0, -1.0, - -1.0, 1.0, -1.0 + -1.0, -1.0, -1.0, 0.0f, 0.0f, + 1.0, -1.0, -1.0, 1.0f, 0.0f, + 1.0, 1.0, -1.0, 0.0f, 1.0f, + -1.0, 1.0, -1.0, 1.0f, 1.0f, }; GLfloat cube_colors[] = { @@ -83,6 +84,12 @@ Shader BlockRenderer::setShader() { Shader s; s.loadShader(vertsrc, geosrc, fragsrc); s.Bind(); + + Texture t; + t.Bind(); + glUniform1i(s.getUniformHandle("u_texture"), 0); + + return s; } diff --git a/blocks/fragment.shader b/blocks/fragment.shader index 273ce43..8a74ec6 100644 --- a/blocks/fragment.shader +++ b/blocks/fragment.shader @@ -1,9 +1,11 @@ R"(#version 330 core out vec4 color; uniform vec3 u_color; +uniform sampler2D u_texture; in vec3 normal; in vec4 pos; +in vec2 v_texcoords; void main() { @@ -15,6 +17,9 @@ void main() // set light color vec3 diffuse = diff * vec3(1.0,1.0,1.0); - vec3 result = (diffuse) * u_color; - color = vec4(result,0.0); + vec4 c = texture(u_texture, v_texcoords); + + vec3 result = (diffuse) * c.xyz; +// color = vec4(result,0.0); + color = c; })"; \ No newline at end of file diff --git a/blocks/geometry.shader b/blocks/geometry.shader index e3eab01..d663c76 100644 --- a/blocks/geometry.shader +++ b/blocks/geometry.shader @@ -4,7 +4,6 @@ layout(triangles) in; layout(triangle_strip, max_vertices=3) out; out vec3 normal; - out vec4 pos; void main( void ) diff --git a/blocks/vertex.shader b/blocks/vertex.shader index 63cd45b..f4c2d43 100644 --- a/blocks/vertex.shader +++ b/blocks/vertex.shader @@ -1,16 +1,17 @@ R"(#version 330 core // Input vertex data, different for all executions of this shader. layout(location = 0) in vec3 vertexPosition_modelspace; +layout(location = 1) in vec2 texCoords; //// Values that stay constant for the whole mesh. uniform mat4 MVP; uniform mat4 translation; -out vec3 FragPos; +out vec2 v_texcoords; void main(){ // Output position of the vertex, in clip space : MVP * position vec4 pos = MVP * translation * vec4(vertexPosition_modelspace,1); gl_Position = pos; - FragPos = pos.xyz; + v_texcoords = texCoords; })"; \ No newline at end of file diff --git a/bmploader.cpp b/bmploader.cpp new file mode 100644 index 0000000..42d223c --- /dev/null +++ b/bmploader.cpp @@ -0,0 +1,73 @@ +// +// Created by lukas on 05.02.22. +// + +#include +#include +#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; +} diff --git a/bmploader.h b/bmploader.h new file mode 100644 index 0000000..0976301 --- /dev/null +++ b/bmploader.h @@ -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 diff --git a/gl/Texture.cpp b/gl/Texture.cpp new file mode 100644 index 0000000..33fc84b --- /dev/null +++ b/gl/Texture.cpp @@ -0,0 +1,22 @@ +// +// Created by lukas on 06.02.22. +// + +#include "Texture.h" +#include "../bmploader.h" +#define GL_GLEXT_PROTOTYPES + +#include + +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); +} diff --git a/gl/Texture.h b/gl/Texture.h new file mode 100644 index 0000000..edab5ff --- /dev/null +++ b/gl/Texture.h @@ -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 diff --git a/gl/VertexArray.cpp b/gl/VertexArray.cpp index 48df8e4..5bd8ae3 100644 --- a/gl/VertexArray.cpp +++ b/gl/VertexArray.cpp @@ -13,12 +13,14 @@ VertexArray::VertexArray(const VertexBuffer& buff) { buff.Bind(); // generate new vertex array object - glGenVertexArrays(1, &handle); + glGenVertexArrays(2, &handle); Bind(); // 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(1); } unsigned VertexArray::getHandle() const { From 9df12e0fe1d05533734a51d47d23b37a899ac196 Mon Sep 17 00:00:00 2001 From: lukas Date: Sun, 6 Feb 2022 18:28:12 +0100 Subject: [PATCH 2/4] fix texture rendering --- assets/blocks/block.bmp | Bin 0 -> 12342 bytes block.bmp | Bin 12342 -> 0 bytes blocks/BlockRenderer.cpp | 109 ++++++++++++++++++++++++++++++--------- blocks/fragment.shader | 8 +-- blocks/geometry.shader | 4 ++ bmploader.cpp | 13 +++-- gl/Texture.cpp | 2 +- gl/VertexArray.cpp | 6 ++- main.cpp | 2 + 9 files changed, 108 insertions(+), 36 deletions(-) create mode 100644 assets/blocks/block.bmp delete mode 100644 block.bmp diff --git a/assets/blocks/block.bmp b/assets/blocks/block.bmp new file mode 100644 index 0000000000000000000000000000000000000000..fc8b73d09eebe6f46b42ae90d8a4079c09173c6a GIT binary patch literal 12342 zcmdU#O>UG)5JWw`9TMr%+^8R`my>MN77#B40#h`?qEI z_xt0Qho6tfc>gKhzW)FA&%=jM@re1y&-s?d!^4m6@$&M5{Y@U2DW0F7Q@p*sy}!S| zzP>6Txo_$R7H~wY1f~t@N{}n@^z_82ZDTr!-y77GhD&X9d57q^S5}|Giua|>h;EQD66pZdI0dsNe@RcZobhQMrvlKCS`9o2eiTeUT5(<;mq zF|r@#H`T71vxK|0oh@K-jw;SC($q>vo4yVNv-QlkBXISz9Dfj$gcvX#Wn{a5jZ`PA z@L7g(Mzc6yK6>&N{XMSb>`=|>*u7*a*T_w}ZJ^)8wmJDj%*x}a!JOLnatpMV)^h9D zs?-{(Q+1R?D-b&U_QT6Gm=&PR>WcQpgl%L;3K0gxF-NQy$7G1K!g`dk5m(^1aGEfP z7S5@TE@-X@q45)6nbfy7D1S}s4%%?yEA!{~A8yx?KB>?;s(OGKZBvkgs3j{$ON@y> z5XHmHfsc{F95to71u#zpZ57VZ%8zLs4GtHrf~`+#Xl_RY+uAmwX2#V?eN)@D9Wg}pq7*lICr9Ne(76m#1Xy)$ z+CO?~1LZ1aY&F$oTBe#2(bRUs+MsKrXRMvtCM{Dqg!D?jxCI-wM0_Dn9zug@~Ay6(3+Z3 z-NLPTIaS-Js-xK<7-A;dX~yel)pq$hg6&=Hbwi|<;E>WzHC2<^iXrs}UO(&Yrt9Va zI>Jn$qpAm((KZEnBD78U6JMFsw>Bt$P3sQYaN;X-m;G&l)&9C0krqx92GPP=tAJ`_ z__a$jLQ|<3wL<0(6=nwGzp% zrubIN5g~a4#vC5C>M#O!uPh6s$TiH=5qlbgA7U8Qsg_%?9=WBC28Wc2fwz&S(8kP1 zQmwEbSyt6XFb%dNSSt+HBQGu5U~6g|-I8f?1+WRj+A53Wu_aGTZ9Cy%GI)u*caL889>^YyQ{ zpP#Ss=O6L&_y0e?z5O04J~99DXa1z|_V(}b=llETIPnX7kADAUovFvcTK|!%$610Q z>Pfd8s&1tcCTrrdO8HB82dLVyc1pI2mRFOAnh*5j9)gqDk0G^Vms5I7+zJM4@q8{N z@C(z9e!PEjO3yB86}*lx5js_U2`H@k{rn$k(sLr{Q5X}kPi-%$LowY?T(?&pn3)7s zD5E|a0Oy&w>;Y)Sv{d1i0l1u=lWkR+l&Dzv80{p}wuEUbGy2-+J?>IKJ|#0iKD=Dg{Dwf2`BdzTHTB?%92MQl-p; z`!ycgzIZzOk^7YX1%t75_;<4et#Ij?MpR8e_%OC^@~KiF{_gOXPGwFsD|Q$x1Y~Qr z(;#1XfBVmvy{(u|RsZgVWZR94+1^hZA~AaKaYA5*(EHU_UiHMJucYb2`{~a(&%465 z^VFVd;w(ZNb{!=d^sV-Gs{f}#HHocS^S)ZD%({z*xJz?fsY7bU;=h>w} z%qkSiX8rZ4d^(oOU<(`BGS$>notk6t$^F@2L930Y0;wxg9ZOZ9Iz(c0et*COLu4&+ z9WiC~G@X2ctsvD-)8sH^2&RY;n2t1(TBnoFs+z414RwUWVK^zsh6pDyGc6KN1zJ6= zt5s8*kyD{=&xksqt;e1Q^PJXMyxmAiiZ-IA@%mJ3*P05w!#cI#^wfyy2N7$?r}eZ< zn*8mw5i@x%g)oQ(UPi=IOl01F2^(*v5Q|csNombr- z{k4j9_1fwHn^00m>DQ$@uhW<7dR|>|!jQTJlX@&p)}1hV0KA%?r&$+b+H%5>1;+VQ t!Mc~{sXTQzNi2I diff --git a/blocks/BlockRenderer.cpp b/blocks/BlockRenderer.cpp index ef71562..cd5e646 100644 --- a/blocks/BlockRenderer.cpp +++ b/blocks/BlockRenderer.cpp @@ -10,40 +10,101 @@ #include VertexArray *BlockRenderer::setVertexArray() { + // float cube_vertices[] = { + // // front + // -1.0, -1.0, 1.0, 0.0f, 0.0f, + // 1.0, -1.0, 1.0, 1.0f, 0.0f, + // 1.0, 1.0, 1.0, 0.0f, 1.0f, + // -1.0, 1.0, 1.0, 1.0f, 1.0f, + // // back + // -1.0, -1.0, -1.0, 0.0f, 0.0f, + // 1.0, -1.0, -1.0, 1.0f, 0.0f, + // 1.0, 1.0, -1.0, 0.0f, 1.0f, + // -1.0, 1.0, -1.0, 1.0f, 1.0f, + // }; + float cube_vertices[] = { // front - -1.0, -1.0, 1.0, 0.0f, 0.0f, - 1.0, -1.0, 1.0, 1.0f, 0.0f, - 1.0, 1.0, 1.0, 0.0f, 1.0f, - -1.0, 1.0, 1.0, 1.0f, 1.0f, + -1, -1, 1, 0, 0, + 1, -1, 1, 1, 0, + 1, 1, 1, 0, 1, + -1, 1, 1, 1, 1, // back - -1.0, -1.0, -1.0, 0.0f, 0.0f, - 1.0, -1.0, -1.0, 1.0f, 0.0f, - 1.0, 1.0, -1.0, 0.0f, 1.0f, - -1.0, 1.0, -1.0, 1.0f, 1.0f, + -1, -1, -1, 0, 0, + 1, -1, -1, 1, 0, + 1, 1, -1, 0, 1, + -1, 1, -1, 1, 1, }; - GLfloat cube_colors[] = { - // front colors - 1.0, 0.0, 0.0, - 0.0, 1.0, 0.0, - 0.0, 0.0, 1.0, - 1.0, 1.0, 1.0, - // back colors - 1.0, 0.0, 0.0, - 0.0, 1.0, 0.0, - 0.0, 0.0, 1.0, - 1.0, 1.0, 1.0 - }; + float cubee[] = { + // face #1 + 1, 1, 1, 1, 0, + -1, 1, 1, 1, 1, + -1, -1, 1, 0, 1, + 1, -1, 1, 0, 0, + + // face #2 + 1, 1, 1, 1, 0, + 1, -1, 1, 1, 1, + 1, -1, -1, 0, 1, + 1, 1, -1, 0, 0, + + // face #3 + 1, 1, 1, 1, 0, + 1, 1, -1, 1, 1, + -1, 1, -1, 0, 1, + -1, 1, 1, 0, 0, + + // face #4 + -1, -1, -1, 1, 0, + -1, 1, -1, 1, 1, + 1, 1, -1, 0, 1, + 1, -1, -1, 0, 0, + + // face #5 + -1, -1, -1, 1, 0, + -1, -1, 1, 1, 1, + -1, 1, 1, 0, 1, + -1, 1, -1, 0, 0, + + // face #6 + -1, -1, -1, 1, 0, + 1, -1, -1, 1, 1, + 1, -1, 1, 0, 1, + -1, -1, 1, 0, 0,}; // Generate a vertex buffer - auto *vb = new VertexBuffer(cube_vertices, sizeof(cube_vertices)); - - // + auto *vb = new VertexBuffer(cubee, sizeof(cubee)); return new VertexArray(*vb); } IndexBuffer *BlockRenderer::setIndexBuffer() { + unsigned indexx[] = { + 0, 1, 2, + 2, 3, 0, + + 4, 5, 6, + 6, 7, 4, + + 8, 9, 10, + 10, 11, 8, + + 12, 13, 14, + 14, 15, 12, + + 16, 17, 18, + 18, 19, 16, + + 20, 21, 22, + 22, 23, 20, + + 24, 25, 26, + 26, 27, 24, + + 28, 29, 30, + 30, 31, 28 + }; + unsigned cube_elements[] = { // front 0, 1, 2, @@ -65,7 +126,7 @@ IndexBuffer *BlockRenderer::setIndexBuffer() { 6, 7, 3 }; - return new IndexBuffer(cube_elements, 36);; + return new IndexBuffer(indexx, 48); } Shader BlockRenderer::setShader() { diff --git a/blocks/fragment.shader b/blocks/fragment.shader index 8a74ec6..e52c309 100644 --- a/blocks/fragment.shader +++ b/blocks/fragment.shader @@ -5,7 +5,7 @@ uniform sampler2D u_texture; in vec3 normal; in vec4 pos; -in vec2 v_texcoords; +in vec2 texcoords; void main() { @@ -17,9 +17,9 @@ void main() // set light color vec3 diffuse = diff * vec3(1.0,1.0,1.0); - vec4 c = texture(u_texture, v_texcoords); + vec4 c = texture(u_texture, texcoords); vec3 result = (diffuse) * c.xyz; -// color = vec4(result,0.0); - color = c; + color = vec4(result,0.0); +// color = c; })"; \ No newline at end of file diff --git a/blocks/geometry.shader b/blocks/geometry.shader index d663c76..afcfba4 100644 --- a/blocks/geometry.shader +++ b/blocks/geometry.shader @@ -6,6 +6,9 @@ layout(triangle_strip, max_vertices=3) out; out vec3 normal; out vec4 pos; +in vec2 v_texcoords[]; +out vec2 texcoords; + void main( void ) { vec3 a = ( gl_in[1].gl_Position - gl_in[0].gl_Position ).xyz; @@ -17,6 +20,7 @@ void main( void ) gl_Position = gl_in[i].gl_Position; normal = N; pos = gl_in[i].gl_Position; + texcoords = v_texcoords[i]; EmitVertex( ); } diff --git a/bmploader.cpp b/bmploader.cpp index 42d223c..ff89f16 100644 --- a/bmploader.cpp +++ b/bmploader.cpp @@ -1,9 +1,10 @@ // // Created by lukas on 05.02.22. // - +#define GL_GLEXT_PROTOTYPES #include #include +#include #include "bmploader.h" unsigned bmploader::loadBMP(const char *imagepath) { @@ -54,18 +55,20 @@ unsigned bmploader::loadBMP(const char *imagepath) { // Create one OpenGL texture unsigned textureID; glGenTextures(1, &textureID); - + glActiveTexture(GL_TEXTURE0); // "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); + 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); - + // Generate mipmaps, by the way. + glGenerateMipmap(GL_TEXTURE_2D); diff --git a/gl/Texture.cpp b/gl/Texture.cpp index 33fc84b..86c15c9 100644 --- a/gl/Texture.cpp +++ b/gl/Texture.cpp @@ -9,7 +9,7 @@ #include Texture::Texture() { - mTexturehandle = bmploader().loadBMP("../block.bmp"); + mTexturehandle = bmploader().loadBMP("../assets/blocks/block.bmp"); } void Texture::Bind() { diff --git a/gl/VertexArray.cpp b/gl/VertexArray.cpp index 5bd8ae3..e3b3c2a 100644 --- a/gl/VertexArray.cpp +++ b/gl/VertexArray.cpp @@ -13,13 +13,15 @@ VertexArray::VertexArray(const VertexBuffer& buff) { buff.Bind(); // generate new vertex array object - glGenVertexArrays(2, &handle); + glGenVertexArrays(1, &handle); + Bind(); // specify syntax of my data 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); + + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) 12); glEnableVertexAttribArray(1); } diff --git a/main.cpp b/main.cpp index cfeda65..ee43dd0 100644 --- a/main.cpp +++ b/main.cpp @@ -94,6 +94,8 @@ int main() { #endif glEnable(GL_DEPTH_TEST); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + GrasBlock gb(0, 0, 0); BaseBlock bb(0.0f, (float) rand() / RAND_MAX, 1.0f, 1, 0, 0); From 68f28a32947b9f804510c18d1745711aa50e12d6 Mon Sep 17 00:00:00 2001 From: lukas Date: Sun, 6 Feb 2022 22:53:29 +0100 Subject: [PATCH 3/4] load different textures --- CMakeLists.txt | 4 +- assets/blocks/dirt.bmp | Bin 0 -> 12342 bytes assets/blocks/{block.bmp => stone.bmp} | Bin blocks/BaseBlock.cpp | 8 +-- blocks/BaseBlock.h | 7 ++- blocks/BlockRenderer.cpp | 58 ++---------------- blocks/DirtBlock.cpp | 9 +++ blocks/DirtBlock.h | 18 ++++++ blocks/GrasBlock.cpp | 8 --- blocks/GrasBlock.h | 18 ------ blocks/RenderBase.h | 39 +++++++------ blocks/Stoneblock.cpp | 8 +++ blocks/Stoneblock.h | 17 ++++++ blocks/TextureLoader.cpp | 22 +++++++ blocks/TextureLoader.h | 23 ++++++++ crosshair/CrossHair.cpp | 49 ++++++++++++++++ crosshair/CrossHair.h | 23 ++++++++ crosshair/fragment.shader | 8 +++ crosshair/vertex.shader | 9 +++ gl/Camera.cpp | 6 +- gl/Shader.cpp | 78 +++++++++++++++++-------- gl/Shader.h | 17 +++--- gl/Texture.cpp | 6 +- gl/Texture.h | 3 + gl/VertexArray.cpp | 17 ++++-- gl/VertexArray.h | 2 + main.cpp | 43 ++++++++------ 27 files changed, 335 insertions(+), 165 deletions(-) create mode 100644 assets/blocks/dirt.bmp rename assets/blocks/{block.bmp => stone.bmp} (100%) create mode 100644 blocks/DirtBlock.cpp create mode 100644 blocks/DirtBlock.h delete mode 100644 blocks/GrasBlock.cpp delete mode 100644 blocks/GrasBlock.h create mode 100644 blocks/Stoneblock.cpp create mode 100644 blocks/Stoneblock.h create mode 100644 blocks/TextureLoader.cpp create mode 100644 blocks/TextureLoader.h create mode 100644 crosshair/CrossHair.cpp create mode 100644 crosshair/CrossHair.h create mode 100644 crosshair/fragment.shader create mode 100644 crosshair/vertex.shader diff --git a/CMakeLists.txt b/CMakeLists.txt index d0a5966..aeccd94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,12 +24,12 @@ SET(srcs main.cpp gl/Shader.cpp gl/Shader.h gl/VertexArray.cpp gl/VertexArray.h gl/Renderer.cpp gl/Renderer.h blocks/BaseBlock.cpp blocks/BaseBlock.h - blocks/GrasBlock.cpp blocks/GrasBlock.h + blocks/DirtBlock.cpp blocks/DirtBlock.h blocks/BlockRenderer.cpp blocks/BlockRenderer.h blocks/RenderBase.cpp blocks/RenderBase.h gl/Camera.cpp gl/Camera.h bmploader.cpp bmploader.h - gl/Texture.cpp gl/Texture.h) + gl/Texture.cpp gl/Texture.h blocks/TextureLoader.cpp blocks/TextureLoader.h blocks/Stoneblock.cpp blocks/Stoneblock.h crosshair/CrossHair.cpp crosshair/CrossHair.h) add_executable(opengltest ${srcs}) diff --git a/assets/blocks/dirt.bmp b/assets/blocks/dirt.bmp new file mode 100644 index 0000000000000000000000000000000000000000..c37bd80b62377078d66555fd5eacddd245dceb74 GIT binary patch literal 12342 zcmb7|v92b?4uqFS*a#9xNQf3hu+oYIK}^P5pnd-oyFa_S+CBd-K(j5kU9Kv7=A3(U z|M=_kFCW%_pX2-~{{3G6zWMN7MDbtLfBeKh5+6Q%{POkNpZ|szRT%Pmt2%RmQ__Z2W zl6JZjJ6KZ?s8rx4L4sspM3r6VS0s$66eO~rBIxj!AAaRQ zg8Yc{ijyTIo?BwKpFf6|311gWYRdG0wo+Q>;!7XS0gDqe@#Ccka*i?-mtpR}CxPJ?or4)%85Fsq;^%;+L@>9# zr~+1y)dA=aKuB?pAR8O35`?-&;Qnt;;RyuDYrpZ~Uq}ZZifzoL0jMR7^#`v~W%>&d zr+Du6jS125^v6*AYga)vo~XL6NW5!cC}lF(&hJM^VLSjF8UD8(EdZ{Y@oBy=m_HTw zIF^@C(XPfIQGOGyrBNzyXCCm^s}Ux9rE>l;Q(6sRG&$KpPlpY<#tC@0^vH*eP7bgz zgY&-L%Xdq|zi1tEbCPk4$08x1Gysl)O<*ta7JLM;C9J=zNcta~PDmVpk(3`VC1)>O zjUa^|Jh}?6VB3q2p?_^kE(!HZsRxN(0mPHZ6A;(i&w*c`Js6#XdHMMR@POGv4LuFiX8ky~&hr^*P9C$$2Jn zF+{Sgo-2Rz5MUmss!T7+ zF=K??)!QCyaec7e8}LCkOz-9NoK!s&F&l_R9swfoXR9nihpAWUN24**g@KH_C&oZtNN~A0c(?)y!=O!UY(e~|FBQd=FgaAuDNI#o`t!C z>Xoq<%h_%}s!xG$XpLsENcMnVl5aw+)p}_U6|%<7-SC zjX^@^kWTeAEI~GQZ)N0)`~nlgn(fWurP7utto>< z!C>=2TuAu^qeW)~88ce*)O9(${R>>ZtrgcYX(NO2Q`?KA$l)ZDadQexQu#y*({hDj zmN2hz7Si#=0dSs(?<+s=lnT&qzpn1qSB(t=hbYf(1k?T_UzTv*BV@jLhUc8o+j}QS z#QDbmv5+Q#Fi+H_yUar?Od9jN$2}SV`OlWqGn~bI`S)sJ+G<=iLdH^T{+eRB67o*U zPg;#$zLsVyQSG7gfu!W8R6KddDfBccPGC;VGUH$YNESxk&X%Z3T5+}X6!`;d|2MK= zYPaR;(98)~D>`7EVYDF6K)s6-e75()ZxuPgFbsM2lWb;5cn{zN>EP|5*@{0r z$X=U=P`Vq?hQ;i1vJs7GKtj>AVq6LK7(l3#7G1+hW||oW*;A?Vty}mP=7T&NZLujQ z!>yGW!yw_Oc(T2bQ~KOY`Lz8P-n3xz6Sq>4mds?Sg%aE`fnW;6AWlkY#+D}9s`iZH zi6C40(KUkw78@C^5s$}(#P$ZPcIQtC^DhiRAQNzn5G^!v4t4Juy+Vo>s<`SIEkX)F zM&EOC_Wm97Do$|w$4?UW>wu~x-Ve@lR-1VK*FO_rB?xpqbkD+0QeQH z$itB5GUjUm53=Id=1amx7_J_#TX*e8D#d1-luz*tW7VS1vU`z;Dg#!qxvKrO1WbS5YX7@8XhC*wl&IF}{?#s6WCFAE?9j#>f4L>Ts~L;D zljw?2L3Hsbqaa(>`*i@WGSja$;@z_1-3Y@X35<{sa^*AgamXiTwTt{s;Din2H5ozttv1JnE8 zY_>@aPRlccjEU;Z!0e(CC)L95(-w|UoFGT9UkABL1TaLbL6i}4X!$iT+g`6F0c+b% zMdlT!=M_7-Cq2eYB73FkQV`O2%Yk2p3$G=2*F?Hd?;lK@1B~u4FwFQB4|mra27$rE SDI3pbZvQ=Ls#{gpTm4^QDV?hT literal 0 HcmV?d00001 diff --git a/assets/blocks/block.bmp b/assets/blocks/stone.bmp similarity index 100% rename from assets/blocks/block.bmp rename to assets/blocks/stone.bmp diff --git a/blocks/BaseBlock.cpp b/blocks/BaseBlock.cpp index a96953a..6bc0976 100644 --- a/blocks/BaseBlock.cpp +++ b/blocks/BaseBlock.cpp @@ -12,16 +12,14 @@ #include "BlockRenderer.h" void BaseBlock::render() { - glUniform3f(BlockRenderer::getInstance()->getUniformhandle("u_color"), r, g, b); - glm::mat4 position = glm::translate(glm::mat4(1.0f), glm::vec3((float) xpos * 2, (float) ypos * 2, (float) zpos * 2)); glUniformMatrix4fv(BlockRenderer::getInstance()->getUniformhandle("translation"), 1, GL_FALSE, &position[0][0]); + texture->Bind(); + BlockRenderer::getInstance()->render(); } -BaseBlock::BaseBlock(float r, float g, float b, uint xpos, uint ypos, uint zpos) : r(r), g(g), b(b), xpos(xpos), ypos(ypos), zpos(zpos) { - // BlockRenderer::init(); - // this->getrenderer().init(); +BaseBlock::BaseBlock(uint xpos, uint ypos, uint zpos, Texture *texture): xpos(xpos), ypos(ypos), zpos(zpos), texture(texture) { } \ No newline at end of file diff --git a/blocks/BaseBlock.h b/blocks/BaseBlock.h index 4fb8d8b..016f230 100644 --- a/blocks/BaseBlock.h +++ b/blocks/BaseBlock.h @@ -7,14 +7,15 @@ #include "BlockRenderer.h" +#include "../gl/Texture.h" class BaseBlock { private: - float r,g,b; - uint xpos,ypos,zpos; + Texture *texture; + uint xpos, ypos, zpos; public: - BaseBlock(float r, float g, float b, uint xpos, uint ypos, uint zpos); + BaseBlock(uint xpos, uint ypos, uint zpos, Texture *texture); void render(); }; diff --git a/blocks/BlockRenderer.cpp b/blocks/BlockRenderer.cpp index cd5e646..5aeb7f3 100644 --- a/blocks/BlockRenderer.cpp +++ b/blocks/BlockRenderer.cpp @@ -10,32 +10,6 @@ #include VertexArray *BlockRenderer::setVertexArray() { - // float cube_vertices[] = { - // // front - // -1.0, -1.0, 1.0, 0.0f, 0.0f, - // 1.0, -1.0, 1.0, 1.0f, 0.0f, - // 1.0, 1.0, 1.0, 0.0f, 1.0f, - // -1.0, 1.0, 1.0, 1.0f, 1.0f, - // // back - // -1.0, -1.0, -1.0, 0.0f, 0.0f, - // 1.0, -1.0, -1.0, 1.0f, 0.0f, - // 1.0, 1.0, -1.0, 0.0f, 1.0f, - // -1.0, 1.0, -1.0, 1.0f, 1.0f, - // }; - - float cube_vertices[] = { - // front - -1, -1, 1, 0, 0, - 1, -1, 1, 1, 0, - 1, 1, 1, 0, 1, - -1, 1, 1, 1, 1, - // back - -1, -1, -1, 0, 0, - 1, -1, -1, 1, 0, - 1, 1, -1, 0, 1, - -1, 1, -1, 1, 1, - }; - float cubee[] = { // face #1 1, 1, 1, 1, 0, @@ -75,7 +49,11 @@ VertexArray *BlockRenderer::setVertexArray() { // Generate a vertex buffer auto *vb = new VertexBuffer(cubee, sizeof(cubee)); - return new VertexArray(*vb); + auto *va = new VertexArray(*vb); + va->add(0,3,5,0); + va->add(1,2,5,12); + + return va; } IndexBuffer *BlockRenderer::setIndexBuffer() { @@ -105,27 +83,6 @@ IndexBuffer *BlockRenderer::setIndexBuffer() { 30, 31, 28 }; - unsigned cube_elements[] = { - // front - 0, 1, 2, - 2, 3, 0, - // right - 1, 5, 6, - 6, 2, 1, - // back - 7, 6, 5, - 5, 4, 7, - // left - 4, 0, 3, - 3, 7, 4, - // bottom - 4, 5, 1, - 1, 0, 4, - // top - 3, 2, 6, - 6, 7, 3 - }; - return new IndexBuffer(indexx, 48); } @@ -146,11 +103,6 @@ Shader BlockRenderer::setShader() { s.loadShader(vertsrc, geosrc, fragsrc); s.Bind(); - Texture t; - t.Bind(); - glUniform1i(s.getUniformHandle("u_texture"), 0); - - return s; } diff --git a/blocks/DirtBlock.cpp b/blocks/DirtBlock.cpp new file mode 100644 index 0000000..60eb78f --- /dev/null +++ b/blocks/DirtBlock.cpp @@ -0,0 +1,9 @@ +// +// Created by lukas on 04.02.22. +// + +#include "DirtBlock.h" +#include "TextureLoader.h" + +DirtBlock::DirtBlock(const uint &xpos, const uint &ypos, const uint &zpos) : BaseBlock(xpos, ypos, zpos, TextureLoader::loadTexture("../assets/blocks/dirt.bmp")) { +} diff --git a/blocks/DirtBlock.h b/blocks/DirtBlock.h new file mode 100644 index 0000000..c5aa2d0 --- /dev/null +++ b/blocks/DirtBlock.h @@ -0,0 +1,18 @@ +// +// Created by lukas on 04.02.22. +// + +#ifndef OPENGLTEST_DIRTBLOCK_H +#define OPENGLTEST_DIRTBLOCK_H + + +#include +#include "BaseBlock.h" + +class DirtBlock : public BaseBlock { +public: + DirtBlock(const uint &xpos, const uint &ypos, const uint &zpos); +}; + + +#endif //OPENGLTEST_DIRTBLOCK_H diff --git a/blocks/GrasBlock.cpp b/blocks/GrasBlock.cpp deleted file mode 100644 index 4d39f76..0000000 --- a/blocks/GrasBlock.cpp +++ /dev/null @@ -1,8 +0,0 @@ -// -// Created by lukas on 04.02.22. -// - -#include "GrasBlock.h" - -GrasBlock::GrasBlock(const uint &xpos, const uint &ypos, const uint &zpos) : BaseBlock(0.0f, 1.0f, 0.0f, xpos, ypos, zpos) { -} diff --git a/blocks/GrasBlock.h b/blocks/GrasBlock.h deleted file mode 100644 index b668e5d..0000000 --- a/blocks/GrasBlock.h +++ /dev/null @@ -1,18 +0,0 @@ -// -// Created by lukas on 04.02.22. -// - -#ifndef OPENGLTEST_GRASBLOCK_H -#define OPENGLTEST_GRASBLOCK_H - - -#include -#include "BaseBlock.h" - -class GrasBlock : public BaseBlock { -public: - GrasBlock(const uint &xpos, const uint &ypos, const uint &zpos); -}; - - -#endif //OPENGLTEST_GRASBLOCK_H diff --git a/blocks/RenderBase.h b/blocks/RenderBase.h index 4d108d7..104e8b5 100644 --- a/blocks/RenderBase.h +++ b/blocks/RenderBase.h @@ -6,17 +6,22 @@ #define OPENGLTEST_RENDERBASE_H +#include #include "../gl/Renderer.h" #include "../gl/Shader.h" -template +template class RenderBase { +private: + static RenderBase *instance; public: - virtual VertexArray* setVertexArray() = 0; - virtual Shader setShader() = 0; - virtual IndexBuffer* setIndexBuffer() = 0; + virtual VertexArray *setVertexArray() = 0; - static RenderBase* getInstance() { + virtual Shader setShader() = 0; + + virtual IndexBuffer *setIndexBuffer() = 0; + + static RenderBase *getInstance() { if (instance == nullptr) { instance = new T(); instance->init(); @@ -25,32 +30,32 @@ public: } -private: - static RenderBase* instance; - - -private: +public: Renderer r; - VertexArray* va; - IndexBuffer* ib; + VertexArray *va; + IndexBuffer *ib; Shader s; public: - RenderBase(){}; - void render(){ + RenderBase() {}; + + void render() { r.render(*va, *ib, s); } + void init() { s = setShader(); va = setVertexArray(); ib = setIndexBuffer(); } - void deinit(){} - unsigned getMVPhandle(){ + void deinit() {} + + unsigned getMVPhandle() { return s.getUniformHandle("MVP"); } - unsigned getUniformhandle(std::string name){ + + unsigned getUniformhandle(std::string name) { return s.getUniformHandle(std::move(name)); } }; diff --git a/blocks/Stoneblock.cpp b/blocks/Stoneblock.cpp new file mode 100644 index 0000000..a74d0a4 --- /dev/null +++ b/blocks/Stoneblock.cpp @@ -0,0 +1,8 @@ +// +// Created by lukas on 06.02.22. +// + +#include "Stoneblock.h" +#include "TextureLoader.h" + +Stoneblock::Stoneblock(uint xpos, uint ypos, uint zpos) : BaseBlock(xpos, ypos, zpos, TextureLoader::loadTexture("../assets/blocks/stone.bmp")) {} diff --git a/blocks/Stoneblock.h b/blocks/Stoneblock.h new file mode 100644 index 0000000..04340c9 --- /dev/null +++ b/blocks/Stoneblock.h @@ -0,0 +1,17 @@ +// +// Created by lukas on 06.02.22. +// + +#ifndef OPENGLTEST_STONEBLOCK_H +#define OPENGLTEST_STONEBLOCK_H + + +#include "BaseBlock.h" + +class Stoneblock: public BaseBlock { +public: + Stoneblock(uint xpos, uint ypos, uint zpos); +}; + + +#endif //OPENGLTEST_STONEBLOCK_H diff --git a/blocks/TextureLoader.cpp b/blocks/TextureLoader.cpp new file mode 100644 index 0000000..a379df6 --- /dev/null +++ b/blocks/TextureLoader.cpp @@ -0,0 +1,22 @@ +// +// Created by lukas on 06.02.22. +// + +#include +#include "TextureLoader.h" + +std::unordered_map TextureLoader::texts; + +Texture *TextureLoader::loadTexture(std::string path) { + if (texts.find(path) != texts.end()) { + return &texts[path]; + } else { + std::cout << "Loading texture: " << path << std::endl; + auto *t = new Texture; + t->load(path); + t->Bind(); + + texts[path] = *t; + return t; + } +} diff --git a/blocks/TextureLoader.h b/blocks/TextureLoader.h new file mode 100644 index 0000000..35d149d --- /dev/null +++ b/blocks/TextureLoader.h @@ -0,0 +1,23 @@ +// +// Created by lukas on 06.02.22. +// + +#ifndef OPENGLTEST_TEXTURELOADER_H +#define OPENGLTEST_TEXTURELOADER_H + + +#include +#include "../gl/Texture.h" + +class TextureLoader { +private: + static std::unordered_map texts; +public: + static Texture* loadTexture(std::string path); + +private: + +}; + + +#endif //OPENGLTEST_TEXTURELOADER_H diff --git a/crosshair/CrossHair.cpp b/crosshair/CrossHair.cpp new file mode 100644 index 0000000..73e2f69 --- /dev/null +++ b/crosshair/CrossHair.cpp @@ -0,0 +1,49 @@ +// +// Created by lukas on 06.02.22. +// +#define GL_GLEXT_PROTOTYPES +#include +#include "CrossHair.h" + +VertexArray *CrossHair::setVertexArray() { + float fade[] = { + // face #1 + -0.02, -0.05, + 0.02, 0.02, + -0.05, 0.05, + }; + + // Generate a vertex buffer + auto *vb = new VertexBuffer(fade, sizeof(fade)); + auto *va = new VertexArray(*vb); + va->add(0,2,3,0); + + return va; +} + +Shader CrossHair::setShader() { + const std::string vertsrc = +#include "vertex.shader" + + const std::string fragsrc = +#include "fragment.shader" + + Shader s; + s.loadShader(vertsrc, "", fragsrc); + s.Bind(); + + return s; +} + +IndexBuffer *CrossHair::setIndexBuffer() { + unsigned indexx[] = { + 0,2,1 + }; + + return new IndexBuffer(indexx, 3); +} + +void CrossHair::renderr() { + glUniform3f(getUniformhandle("u_color"), 1.0f, 1.0f, 1.0f); + RenderBase::render(); +} diff --git a/crosshair/CrossHair.h b/crosshair/CrossHair.h new file mode 100644 index 0000000..636577b --- /dev/null +++ b/crosshair/CrossHair.h @@ -0,0 +1,23 @@ +// +// Created by lukas on 06.02.22. +// + +#ifndef OPENGLTEST_CROSSHAIR_H +#define OPENGLTEST_CROSSHAIR_H + + +#include "../blocks/RenderBase.h" + +class CrossHair : public RenderBase { +public: + VertexArray *setVertexArray() override; + + Shader setShader() override; + + IndexBuffer *setIndexBuffer() override; + + void renderr(); +}; + + +#endif //OPENGLTEST_CROSSHAIR_H diff --git a/crosshair/fragment.shader b/crosshair/fragment.shader new file mode 100644 index 0000000..b002f00 --- /dev/null +++ b/crosshair/fragment.shader @@ -0,0 +1,8 @@ +R"(#version 330 core +out vec4 color; +uniform vec3 u_color; + +void main() +{ + color = vec4(u_color, 0.5); +})"; \ No newline at end of file diff --git a/crosshair/vertex.shader b/crosshair/vertex.shader new file mode 100644 index 0000000..c3c4751 --- /dev/null +++ b/crosshair/vertex.shader @@ -0,0 +1,9 @@ +R"(#version 330 core +// Input vertex data, different for all executions of this shader. +layout(location = 0) in vec2 vertexPosition_modelspace; + +void main(){ + // Output position of the vertex, in clip space : MVP * position + vec4 pos = vec4(vertexPosition_modelspace,0.0,1.0); + gl_Position = pos; +})"; \ No newline at end of file diff --git a/gl/Camera.cpp b/gl/Camera.cpp index 9eb66dd..e68a983 100644 --- a/gl/Camera.cpp +++ b/gl/Camera.cpp @@ -2,6 +2,7 @@ // Created by lukas on 05.02.22. // #define GL_GLEXT_PROTOTYPES + #include #include #include @@ -68,13 +69,16 @@ void Camera::setWindowSize(double width, double height) { void Camera::addRotaion(double rotx, double roty) { rx -= rotx / 300; - ry -= roty / 300; + ry += roty / 300; // limit to 2pi rx = std::fmod(rx, (2 * M_PI)); ry = std::fmod(ry, (2 * M_PI)); + ry = fmin(ry, M_PI / 2.0f); + ry = fmax(ry, -M_PI / 2.0f); + updateCameraPos(); } diff --git a/gl/Shader.cpp b/gl/Shader.cpp index 95e7014..2a51fce 100644 --- a/gl/Shader.cpp +++ b/gl/Shader.cpp @@ -46,9 +46,9 @@ unsigned Shader::getHandle() const { } unsigned Shader::getUniformHandle(std::string name) { - if(uniformhandles.find(name) != uniformhandles.end()){ + if (uniformhandles.find(name) != uniformhandles.end()) { return uniformhandles[name]; - }else{ + } else { const unsigned id = glGetUniformLocation(mProgHandle, name.c_str()); uniformhandles[name] = id; return id; @@ -82,37 +82,56 @@ unsigned int Shader::loadShaderFromFile(const std::string vertex_file_path, cons } unsigned int Shader::loadShader(const std::string vertex_src, const std::string geometry_src, const std::string fragment_src) { - // Compile Vertex Shader - printf("Compiling vertex shader\n"); - uint VertexShaderID = compileShader(vertex_src.c_str(), GL_VERTEX_SHADER); - if (VertexShaderID == 0) { - printf("Error Compiling shader\n"); - return 0; + uint VertexShaderID; + uint GeometryShaderID; + uint FragmentShaderID; + + if (!vertex_src.empty()) { + // Compile Vertex Shader + printf("Compiling vertex shader\n"); + VertexShaderID = compileShader(vertex_src.c_str(), GL_VERTEX_SHADER); + if (VertexShaderID == 0) { + printf("Error Compiling vertex shader\n"); + return 0; + } } - printf("Compiling geometry shader\n"); - uint GeometryShaderID = compileShader(geometry_src.c_str(), GL_GEOMETRY_SHADER); - if (GeometryShaderID == 0) { - printf("Error Compiling shader\n"); - return 0; + + if (!geometry_src.empty()) { + printf("Compiling geometry shader\n"); + GeometryShaderID = compileShader(geometry_src.c_str(), GL_GEOMETRY_SHADER); + if (GeometryShaderID == 0) { + printf("Error Compiling shader\n"); + return 0; + } } - printf("Compiling fragment shader\n"); - uint FragmentShaderID = compileShader(fragment_src.c_str(), GL_FRAGMENT_SHADER); - if (FragmentShaderID == 0) { - printf("Error Compiling shader\n"); - return 0; + if (!fragment_src.empty()) { + printf("Compiling fragment shader\n"); + FragmentShaderID = compileShader(fragment_src.c_str(), GL_FRAGMENT_SHADER); + if (FragmentShaderID == 0) { + printf("Error Compiling shader\n"); + return 0; + } } + // Link the program GLint Result = GL_FALSE; int InfoLogLength; printf("Linking shader program\n"); GLuint ProgramID = glCreateProgram(); - glAttachShader(ProgramID, VertexShaderID); - glAttachShader(ProgramID, GeometryShaderID); - glAttachShader(ProgramID, FragmentShaderID); + if (!vertex_src.empty()) { + glAttachShader(ProgramID, VertexShaderID); + } + if (!geometry_src.empty()) { + glAttachShader(ProgramID, GeometryShaderID); + } + if (!fragment_src.empty()) { + glAttachShader(ProgramID, FragmentShaderID); + } + glLinkProgram(ProgramID); // Check the program @@ -125,11 +144,20 @@ unsigned int Shader::loadShader(const std::string vertex_src, const std::string } // cleanup shaders - glDetachShader(ProgramID, VertexShaderID); - glDetachShader(ProgramID, FragmentShaderID); + if (!vertex_src.empty()){ + glDetachShader(ProgramID, VertexShaderID); + glDeleteShader(VertexShaderID); + } - glDeleteShader(VertexShaderID); - glDeleteShader(FragmentShaderID); + if (!geometry_src.empty()){ + glDetachShader(ProgramID, GeometryShaderID); + glDeleteShader(GeometryShaderID); + } + + if (!fragment_src.empty()){ + glDetachShader(ProgramID, FragmentShaderID); + glDeleteShader(FragmentShaderID); + } mProgHandle = ProgramID; return ProgramID; diff --git a/gl/Shader.h b/gl/Shader.h index 1bc93e1..f5fed62 100644 --- a/gl/Shader.h +++ b/gl/Shader.h @@ -24,13 +24,7 @@ public: unsigned int loadShader(const std::string vertex_src, const std::string geometry_src, const std::string fragment_src); - /** - * comile a specific shader source - * @param source source string - * @param type shader tpye - * @return id of copiled shader - */ - unsigned int compileShader(const char *source, unsigned int type) const; + std::string readFile(std::string path); @@ -40,6 +34,15 @@ public: unsigned getUniformHandle(std::string name); +private: + /** + * comile a specific shader source + * @param source source string + * @param type shader tpye + * @return id of copiled shader + */ + unsigned int compileShader(const char *source, unsigned int type) const; + }; #endif //OPENGLTEST_SHADER_H diff --git a/gl/Texture.cpp b/gl/Texture.cpp index 86c15c9..bd93de8 100644 --- a/gl/Texture.cpp +++ b/gl/Texture.cpp @@ -4,12 +4,12 @@ #include "Texture.h" #include "../bmploader.h" + #define GL_GLEXT_PROTOTYPES #include Texture::Texture() { - mTexturehandle = bmploader().loadBMP("../assets/blocks/block.bmp"); } void Texture::Bind() { @@ -20,3 +20,7 @@ void Texture::Bind() { void Texture::UnBind() { glBindTexture(GL_TEXTURE_2D, 0); } + +void Texture::load(std::string path) { + mTexturehandle = bmploader().loadBMP(path.c_str()); +} diff --git a/gl/Texture.h b/gl/Texture.h index edab5ff..dba49d5 100644 --- a/gl/Texture.h +++ b/gl/Texture.h @@ -6,11 +6,14 @@ #define OPENGLTEST_TEXTURE_H +#include + class Texture { private: unsigned mTexturehandle; public: Texture(); + void load(std::string path); void Bind(); void UnBind(); }; diff --git a/gl/VertexArray.cpp b/gl/VertexArray.cpp index e3b3c2a..64fd8d9 100644 --- a/gl/VertexArray.cpp +++ b/gl/VertexArray.cpp @@ -17,14 +17,19 @@ VertexArray::VertexArray(const VertexBuffer& buff) { Bind(); - // specify syntax of my data - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) nullptr); - glEnableVertexAttribArray(0); - - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) 12); - glEnableVertexAttribArray(1); +// // specify syntax of my data +// glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) nullptr); +// glEnableVertexAttribArray(0); +// +// glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *) 12); +// glEnableVertexAttribArray(1); } unsigned VertexArray::getHandle() const { return handle; } + +void VertexArray::add(int index, int size, int stride, int pos) { + glVertexAttribPointer(index, size, GL_FLOAT, GL_FALSE, stride * sizeof(float), (void *) pos); + glEnableVertexAttribArray(index); +} diff --git a/gl/VertexArray.h b/gl/VertexArray.h index a4806bb..3f38b91 100644 --- a/gl/VertexArray.h +++ b/gl/VertexArray.h @@ -17,6 +17,8 @@ public: void Bind() const; + void add(int index, int size, int stride, int pos); + unsigned getHandle() const; }; diff --git a/main.cpp b/main.cpp index ee43dd0..b3489aa 100644 --- a/main.cpp +++ b/main.cpp @@ -3,15 +3,16 @@ #define GL_GLEXT_PROTOTYPES #include -#include #include #include "gl/Shader.h" #include "gl/IndexBuffer.h" #include "gl/VertexArray.h" #include "gl/Renderer.h" -#include "blocks/GrasBlock.h" +#include "blocks/DirtBlock.h" #include "blocks/BlockRenderer.h" #include "gl/Camera.h" +#include "blocks/Stoneblock.h" +#include "crosshair/CrossHair.h" #include #include @@ -96,24 +97,18 @@ int main() { glEnable(GL_DEPTH_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - GrasBlock gb(0, 0, 0); - BaseBlock bb(0.0f, (float) rand() / RAND_MAX, 1.0f, 1, 0, 0); - - // BlockRenderer::getInstance()->init(); - std::vector blocks; - - blocks.push_back(gb); - blocks.push_back(bb); - blocks.emplace_back(1.0f, .0f, 1.0f, 0, 0, 1); - - for (int i = 0; i < 5; i++) { - blocks.push_back(GrasBlock(i, i, 0)); + for (int x = 0; x < 15; x++) { + for (int y = 0; y < 15; ++y) { + blocks.push_back(DirtBlock(x, y, 1)); + blocks.push_back(Stoneblock(x, y, 0)); + } } - cam.setPos(0, 0, 0); - cam.setRotation(0,0); + cam.setPos(0, 0, 3); + cam.setRotation(0, 0); + + CrossHair::getInstance()->s.Bind(); while (!glfwWindowShouldClose(window)) { // process user input events @@ -122,10 +117,20 @@ int main() { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + + + glUniform3f(CrossHair::getInstance()->getUniformhandle("u_color"), 0.0f, 0.0f, 0.0f); + CrossHair::getInstance()->render(); + + CrossHair::getInstance()->s.Bind(); + glBindTexture(GL_TEXTURE_2D, 0); + for (auto b: blocks) { b.render(); } + // swap buffer to calc and show glfwSwapBuffers(window); // poll for and execute events @@ -167,11 +172,11 @@ void processInput(GLFWwindow *window) { } if (glfwGetKey(window, GLFW_KEY_SPACE)) { - cam.addPos(0.0,0.0,0.1); + cam.addPos(0.0, 0.0, 0.1); } if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT)) { - cam.addPos(0.0,0.0,-0.1); + cam.addPos(0.0, 0.0, -0.1); } } From 360a1cc79de3f27a3499c587978afe079833ef3d Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 10 Feb 2022 18:24:56 +0100 Subject: [PATCH 4/4] add crosshair blockgen with sin --- CMakeLists.txt | 2 +- blocks/AirBlock.h | 18 +++++++++++++++++ blocks/BaseBlock.cpp | 5 ++--- blocks/BaseBlock.h | 4 +++- blocks/RenderBase.h | 17 +++++++++++----- crosshair/CrossHair.cpp | 3 ++- crosshair/CrossHair.h | 2 +- gl/Camera.cpp | 12 +++++++++++ gl/Camera.h | 4 ++++ gl/Shader.cpp | 1 + gl/Shader.h | 7 ++++++- main.cpp | 44 +++++++++++++++++++++++++++++------------ 12 files changed, 93 insertions(+), 26 deletions(-) create mode 100644 blocks/AirBlock.h diff --git a/CMakeLists.txt b/CMakeLists.txt index aeccd94..8f2888d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ SET(srcs main.cpp gl/Shader.cpp gl/Shader.h blocks/RenderBase.cpp blocks/RenderBase.h gl/Camera.cpp gl/Camera.h bmploader.cpp bmploader.h - gl/Texture.cpp gl/Texture.h blocks/TextureLoader.cpp blocks/TextureLoader.h blocks/Stoneblock.cpp blocks/Stoneblock.h crosshair/CrossHair.cpp crosshair/CrossHair.h) + gl/Texture.cpp gl/Texture.h blocks/TextureLoader.cpp blocks/TextureLoader.h blocks/Stoneblock.cpp blocks/Stoneblock.h crosshair/CrossHair.cpp crosshair/CrossHair.h blocks/AirBlock.h) add_executable(opengltest ${srcs}) diff --git a/blocks/AirBlock.h b/blocks/AirBlock.h new file mode 100644 index 0000000..2754b9e --- /dev/null +++ b/blocks/AirBlock.h @@ -0,0 +1,18 @@ +// +// Created by lukas on 08.02.22. +// + +#ifndef OPENGLTEST_AIRBLOCK_H +#define OPENGLTEST_AIRBLOCK_H + +#include "BaseBlock.h" + +class AirBlock : public BaseBlock{ +public: + AirBlock(uint xpos, uint ypos, uint zpos) : BaseBlock(xpos, ypos, zpos, nullptr) {} + + void render() override { + } +}; + +#endif //OPENGLTEST_AIRBLOCK_H diff --git a/blocks/BaseBlock.cpp b/blocks/BaseBlock.cpp index 6bc0976..ff26004 100644 --- a/blocks/BaseBlock.cpp +++ b/blocks/BaseBlock.cpp @@ -2,9 +2,6 @@ // Created by lukas on 04.02.22. // -#define GL_GLEXT_PROTOTYPES - -#include #include #include #include @@ -12,6 +9,8 @@ #include "BlockRenderer.h" void BaseBlock::render() { + BlockRenderer::getInstance()->s.Bind(); + glm::mat4 position = glm::translate(glm::mat4(1.0f), glm::vec3((float) xpos * 2, (float) ypos * 2, (float) zpos * 2)); glUniformMatrix4fv(BlockRenderer::getInstance()->getUniformhandle("translation"), 1, GL_FALSE, &position[0][0]); diff --git a/blocks/BaseBlock.h b/blocks/BaseBlock.h index 016f230..d9c40d4 100644 --- a/blocks/BaseBlock.h +++ b/blocks/BaseBlock.h @@ -8,6 +8,8 @@ #include "BlockRenderer.h" #include "../gl/Texture.h" +#include +#include class BaseBlock { private: @@ -17,7 +19,7 @@ private: public: BaseBlock(uint xpos, uint ypos, uint zpos, Texture *texture); - void render(); + virtual void render(); }; diff --git a/blocks/RenderBase.h b/blocks/RenderBase.h index 104e8b5..7ebd55f 100644 --- a/blocks/RenderBase.h +++ b/blocks/RenderBase.h @@ -14,6 +14,12 @@ template class RenderBase { private: static RenderBase *instance; +protected: + RenderBase() { + if (instance) throw std::logic_error("Instance already exists"); + instance = this; + } + public: virtual VertexArray *setVertexArray() = 0; @@ -23,11 +29,10 @@ public: static RenderBase *getInstance() { if (instance == nullptr) { - instance = new T(); + new T(); instance->init(); } return instance; - } public: @@ -37,9 +42,9 @@ public: Shader s; public: - RenderBase() {}; - void render() { + + virtual void render() { r.render(*va, *ib, s); } @@ -49,7 +54,9 @@ public: ib = setIndexBuffer(); } - void deinit() {} + void deinit() { + glDeleteProgram(s.getHandle()); + } unsigned getMVPhandle() { return s.getUniformHandle("MVP"); diff --git a/crosshair/CrossHair.cpp b/crosshair/CrossHair.cpp index 73e2f69..5c9d703 100644 --- a/crosshair/CrossHair.cpp +++ b/crosshair/CrossHair.cpp @@ -43,7 +43,8 @@ IndexBuffer *CrossHair::setIndexBuffer() { return new IndexBuffer(indexx, 3); } -void CrossHair::renderr() { +void CrossHair::render() { + s.Bind(); glUniform3f(getUniformhandle("u_color"), 1.0f, 1.0f, 1.0f); RenderBase::render(); } diff --git a/crosshair/CrossHair.h b/crosshair/CrossHair.h index 636577b..f5dbf3b 100644 --- a/crosshair/CrossHair.h +++ b/crosshair/CrossHair.h @@ -16,7 +16,7 @@ public: IndexBuffer *setIndexBuffer() override; - void renderr(); + void render() override; }; diff --git a/gl/Camera.cpp b/gl/Camera.cpp index e68a983..26fb4a2 100644 --- a/gl/Camera.cpp +++ b/gl/Camera.cpp @@ -97,3 +97,15 @@ double Camera::getxangle() const { double Camera::getyangle() const { return ry; } + +double Camera::getXpos() { + return x; +} + +double Camera::getYpos() { + return y; +} + +double Camera::getZpos() { + return z; +} diff --git a/gl/Camera.h b/gl/Camera.h index 50f08c8..f10fc78 100644 --- a/gl/Camera.h +++ b/gl/Camera.h @@ -19,6 +19,10 @@ public: void setPos(double x, double y, double z); void addPos(double x, double y, double z); + double getXpos(); + double getYpos(); + double getZpos(); + void setRotation(double rotx, double roty); void addRotaion(double rotx, double roty); diff --git a/gl/Shader.cpp b/gl/Shader.cpp index 2a51fce..0ced4fe 100644 --- a/gl/Shader.cpp +++ b/gl/Shader.cpp @@ -12,6 +12,7 @@ #include #include #include +#include unsigned int Shader::compileShader(const char *source, unsigned int type) const { unsigned int shaderid = glCreateShader(type); diff --git a/gl/Shader.h b/gl/Shader.h index f5fed62..c5f75c7 100644 --- a/gl/Shader.h +++ b/gl/Shader.h @@ -7,6 +7,12 @@ #include #include +#include +#include +#include +#define GL_GLEXT_PROTOTYPES + +#include class Shader { private: @@ -31,7 +37,6 @@ public: void Bind() const; unsigned getHandle() const; - unsigned getUniformHandle(std::string name); private: diff --git a/main.cpp b/main.cpp index b3489aa..aa54cc0 100644 --- a/main.cpp +++ b/main.cpp @@ -13,10 +13,9 @@ #include "gl/Camera.h" #include "blocks/Stoneblock.h" #include "crosshair/CrossHair.h" +#include "blocks/AirBlock.h" #include -#include - //#define WIREFRAME void framebuffer_size_callback(GLFWwindow *window, int width, int height); @@ -32,6 +31,7 @@ double oldx = 0; double oldy = 0; bool menuopen = false; +std::vector blocks; void cursor_position_callback(GLFWwindow *window, double xpos, double ypos) { if (menuopen)return; @@ -45,6 +45,21 @@ void cursor_position_callback(GLFWwindow *window, double xpos, double ypos) { cam.addRotaion(xdiff, ydiff); } +void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) +{ + if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) + { + double xpos, ypos; + cam.getXpos(); + + blocks.erase(blocks.begin()+1); + + //getting cursor position + glfwGetCursorPos(window, &xpos, &ypos); + std::cout << "Cursor Position at (" << xpos << " : " << ypos << std::endl; + } +} + GLFWwindow *initWindow() { glfwInit(); @@ -75,6 +90,7 @@ GLFWwindow *initWindow() { glfwSetCursorPosCallback(window, cursor_position_callback); + glfwSetMouseButtonCallback(window, mouse_button_callback); if (glfwRawMouseMotionSupported()) glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE); @@ -97,11 +113,11 @@ int main() { glEnable(GL_DEPTH_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - std::vector blocks; - for (int x = 0; x < 15; x++) { - for (int y = 0; y < 15; ++y) { - blocks.push_back(DirtBlock(x, y, 1)); - blocks.push_back(Stoneblock(x, y, 0)); + for (int x = 0; x < 35; x++) { + for (int y = 0; y < 35; ++y) { + blocks.push_back(new AirBlock(x,y,2)); + blocks.push_back(new DirtBlock(x, y, (int)(sin(x / 3.0)*3)+ (int)(cos(y / 2.0)*2)) ); + blocks.push_back(new Stoneblock(x, y, 0)); } } @@ -119,15 +135,14 @@ int main() { - - glUniform3f(CrossHair::getInstance()->getUniformhandle("u_color"), 0.0f, 0.0f, 0.0f); +// glUniform3f(CrossHair::getInstance()->getUniformhandle("u_color"), 0.0f, 0.0f, 0.0f); CrossHair::getInstance()->render(); - CrossHair::getInstance()->s.Bind(); - glBindTexture(GL_TEXTURE_2D, 0); +// CrossHair::getInstance()->s.Bind(); +// glBindTexture(GL_TEXTURE_2D, 0); for (auto b: blocks) { - b.render(); + b->render(); } @@ -137,7 +152,10 @@ int main() { glfwPollEvents(); } - // glDeleteProgram(shaderProgram); + for(auto b : blocks){ + delete b; + } + BlockRenderer::getInstance()->deinit(); glfwTerminate(); return 0;