diff --git a/CMakeLists.txt b/CMakeLists.txt index a2823b4..7e808b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ function(setup_target NAME) set_property(TARGET ${NAME} PROPERTY CXX_STANDARD_REQUIRED ON) target_compile_options(${NAME} PRIVATE -Wall -Wextra) target_compile_options(${NAME} PRIVATE -fdiagnostics-color=always) - target_compile_options(${NAME} PRIVATE $<$:-ggdb -O2>) + target_compile_options(${NAME} PRIVATE $<$:-ggdb -O0>) target_compile_options(${NAME} PRIVATE $<$:-O3 -NDEBUG>) endfunction(setup_target) diff --git a/data/img/planet_mars.png b/data/img/planet_mars.png new file mode 100644 index 0000000..c8be728 Binary files /dev/null and b/data/img/planet_mars.png differ diff --git a/data/img/test.png b/data/img/test.png new file mode 100644 index 0000000..f7d695c Binary files /dev/null and b/data/img/test.png differ diff --git a/data/shader/background.frag b/data/shader/background.frag new file mode 100644 index 0000000..be14c8f --- /dev/null +++ b/data/shader/background.frag @@ -0,0 +1,13 @@ +#version 120 + +uniform sampler2D texture; + +varying vec3 vertex; +varying vec2 uv; + +void main() +{ + vec3 color = texture2D(texture, uv).rgb; + + gl_FragColor = vec4(color, 1.0); +} \ No newline at end of file diff --git a/data/shader/background.vert b/data/shader/background.vert new file mode 100644 index 0000000..3ade43b --- /dev/null +++ b/data/shader/background.vert @@ -0,0 +1,12 @@ +#version 120 +uniform vec2 uvScale; + +varying vec3 vertex; +varying vec2 uv; + +void main() +{ + gl_Position = gl_Vertex; + vertex = gl_Position.xyz; + uv = uvScale * (0.5 + vertex.xy / 2.0); +} \ No newline at end of file diff --git a/game/renderer_polygon_3d/image_texture.cpp b/game/renderer_polygon_3d/image_texture.cpp index 7d14d71..064d11a 100644 --- a/game/renderer_polygon_3d/image_texture.cpp +++ b/game/renderer_polygon_3d/image_texture.cpp @@ -78,14 +78,12 @@ namespace endofthejedi { NULL, NULL, NULL); //update width and height based on png info - m_width = twidth; - m_height = theight; + m_size = glm::vec2(twidth, theight); - int pot_width = (int) pow(2, ceilf(log2f(m_width))); - int pot_height = (int) pow(2, ceilf(log2f(m_height))); + int pot_width = (int) pow(2, ceilf(log2f(m_size.x))); + int pot_height = (int) pow(2, ceilf(log2f(m_size.y))); - m_uv_scale_x = m_width / (float) pot_width; - m_uv_scale_y = m_height / (float) pot_height; + m_uvScale = m_size / glm::vec2(pot_width, pot_height); /*puts("#########################################");*/ /*printf("# PNG: width=%d, height=%d\n", width, height);*/ @@ -111,7 +109,7 @@ namespace endofthejedi { } //row_pointers is for pointing to image_data for reading the png with libpng - png_bytep *row_pointers = (png_bytep *) malloc(sizeof(png_bytep) * m_height); + png_bytep *row_pointers = (png_bytep *) malloc(sizeof(png_bytep) * theight); if (!row_pointers) { //clean up memory and close stuff png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); @@ -121,8 +119,8 @@ namespace endofthejedi { } // set the individual row_pointers to point at the correct offsets of image_data - for (int i = 0; i < m_height; ++i) { - row_pointers[m_height - 1 - i] = image_data + i * pot_rowbytes; + for (uint32_t i = 0; i < theight; ++i) { + row_pointers[theight- 1 - i] = image_data + i * pot_rowbytes; } //read the png into image_data through row_pointers diff --git a/game/renderer_polygon_3d/image_texture.hpp b/game/renderer_polygon_3d/image_texture.hpp index 42657be..35e840b 100644 --- a/game/renderer_polygon_3d/image_texture.hpp +++ b/game/renderer_polygon_3d/image_texture.hpp @@ -46,19 +46,17 @@ namespace endofthejedi { bool valid() const { return m_valid; } - glm::vec2 size() const { return glm::vec2(m_width, m_height); } - glm::vec2 uvScale() const { return glm::vec2(m_uv_scale_x, m_uv_scale_y); } + glm::vec2 size() const { return m_size; } + glm::vec2 uvScale() const { return m_uvScale; } GLuint textureId() const { return m_texture_id; } public: - std::string m_path; // path for the loaded texture is saved here - int m_width; // used width of the pixels of the texture - int m_height; // used height of the pixels of the texture - float m_uv_scale_x; // factor to scale uv-coordinates to get rid of non-power-of-two padding - float m_uv_scale_y; // factor to scale uv-coordinates to get rid of non-power-of-two padding + std::string m_path; // path for the loaded texture is saved here + glm::vec2 m_size; // used width/height of the pixels of the texture + glm::vec2 m_uvScale; // factor to scale uv-coordinates to get rid of non-power-of-two padding - bool m_valid; + bool m_valid; GLuint m_texture_id; // texture binding id }; diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.cpp b/game/renderer_polygon_3d/renderer_polygon_3d.cpp index eec1f89..1caa06c 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.cpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.cpp @@ -20,7 +20,6 @@ namespace endofthejedi { m_shader_game_objects.loadFile(vss_game_objects, GL_VERTEX_SHADER); m_shader_game_objects.loadFile(fss_game_objects, GL_FRAGMENT_SHADER); - std::string vss_particles = "../data/shader/particle.vert"; std::string fss_particles = "../data/shader/particle.frag"; @@ -28,13 +27,23 @@ namespace endofthejedi { m_shader_particles.loadFile(vss_particles, GL_VERTEX_SHADER); m_shader_particles.loadFile(fss_particles, GL_FRAGMENT_SHADER); + std::string vss_background = "../data/shader/background.vert"; + std::string fss_background = "../data/shader/background.frag"; + + m_shader_background.init(); + m_shader_background.loadFile(vss_background, GL_VERTEX_SHADER); + m_shader_background.loadFile(fss_background, GL_FRAGMENT_SHADER); //addModel("../data/mesh/small_atomic_bomb.stl", &m_missileModel); addModel("../data/mesh/rocket.stl", &m_missileModel); addModel("../data/mesh/planet_128.stl", &m_planetModel); addModel("../data/mesh/ship_ufo.stl", &m_shipModel); - m_texture = new ImageTexture("../data/image/planet_mars.png"); + m_texture = new ImageTexture("../data/img/test.png"); + + glActiveTexture(GL_TEXTURE0); + m_texture->loadPng(); + std::cout<<"texture loading: " << m_texture->valid() << std::endl; if (!m_texture->valid()) { exit(-1); @@ -42,7 +51,27 @@ namespace endofthejedi { glm::vec2 s = m_texture->size(); std::cout<<"texture size is " << s.x << " X " << s.y << std::endl; - exit(0); + //exit(0); + } + + void RendererPolygon3d::renderBackgroundImage() + { + m_shader_background.bind(); + + glUniform2fv(m_shader_background.location("uvScale"), 1, glm::value_ptr(m_texture->uvScale())); + + glActiveTexture(GL_TEXTURE0); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, m_texture->textureId()); + + //drawNdcQuad(); + glColor3f(1.0, 0.0, 0.0); + glBegin(GL_QUADS); + glVertex2f(-1.0f, -1.0f); + glVertex2f(1.0f, -1.0f); + glVertex2f(1.0f, 1.0f); + glVertex2f(-1.0f, 1.0f); + glEnd(); } void RendererPolygon3d::render(const game::State *state) @@ -65,6 +94,8 @@ namespace endofthejedi { glClearColor(0.0, 0.0, 0.0, 1.0); + renderBackgroundImage(); + //float s = 0.1; //glClearColor(s, s, 1.2*s, 1.0); diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.hpp b/game/renderer_polygon_3d/renderer_polygon_3d.hpp index 78bfcd4..556183c 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.hpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.hpp @@ -62,6 +62,8 @@ namespace endofthejedi { void configureLightningInShader(Shader *shader) const; + void renderBackgroundImage(); + private: // all models are also here (for easy reloading etc.) std::vector m_models; @@ -72,6 +74,7 @@ namespace endofthejedi { PolygonModel *m_shipModel; // for rendering everything + Shader m_shader_background; Shader m_shader_game_objects; Shader m_shader_particles;