loading texture from png and rendering on the screen (quad) works.
This commit is contained in:
parent
dfda49c027
commit
a56f688204
9 changed files with 76 additions and 21 deletions
|
@ -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 $<$<CONFIG:DEBUG>:-ggdb -O2>)
|
||||
target_compile_options(${NAME} PRIVATE $<$<CONFIG:DEBUG>:-ggdb -O0>)
|
||||
target_compile_options(${NAME} PRIVATE $<$<CONFIG:RELEASE>:-O3 -NDEBUG>)
|
||||
endfunction(setup_target)
|
||||
|
||||
|
|
BIN
data/img/planet_mars.png
Normal file
BIN
data/img/planet_mars.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 MiB |
BIN
data/img/test.png
Normal file
BIN
data/img/test.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7 KiB |
13
data/shader/background.frag
Normal file
13
data/shader/background.frag
Normal file
|
@ -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);
|
||||
}
|
12
data/shader/background.vert
Normal file
12
data/shader/background.vert
Normal file
|
@ -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);
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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<PolygonModel*> 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;
|
||||
|
||||
|
|
Loading…
Reference in a new issue