you can now print images on the screen
This commit is contained in:
@@ -12,7 +12,8 @@ add_executable(${PROJECT_NAME} src/main.cpp
|
||||
include/defines.hpp
|
||||
src/Shader.cpp
|
||||
include/Shader.hpp
|
||||
include/IndexBuffer.hpp)
|
||||
include/IndexBuffer.hpp
|
||||
libs/stb_image.hpp)
|
||||
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:Debug>:_DEBUG>)
|
||||
|
||||
|
||||
@@ -15,9 +15,12 @@ struct VertexBuffer {
|
||||
// POSITION
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,x));
|
||||
// COLOR
|
||||
// TEXTURE COORDINATES
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,r));
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,u));
|
||||
// COLOR
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,r));
|
||||
|
||||
glBindVertexArray(0);
|
||||
};
|
||||
|
||||
@@ -16,5 +16,6 @@ typedef double float64;
|
||||
|
||||
struct Vertex {
|
||||
float32 x, y, z; // position
|
||||
float32 u, v; // texture coordinates
|
||||
float32 r, g, b, a; // color
|
||||
};
|
||||
7462
libs/stb_image.hpp
Normal file
7462
libs/stb_image.hpp
Normal file
File diff suppressed because it is too large
Load Diff
@@ -3,9 +3,12 @@
|
||||
layout(location = 0) out vec4 f_color;
|
||||
|
||||
in vec4 v_color;
|
||||
in vec2 v_texCoord;
|
||||
uniform sampler2D u_texture;
|
||||
|
||||
uniform vec4 u_color;
|
||||
|
||||
void main() {
|
||||
f_color = u_color;
|
||||
vec4 texColor = texture(u_texture, v_texCoord);
|
||||
f_color = texColor;
|
||||
}
|
||||
@@ -1,11 +1,14 @@
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec3 a_position;
|
||||
layout(location = 1) in vec4 a_color;
|
||||
layout(location = 1) in vec2 a_texCoord;
|
||||
layout(location = 2) in vec4 a_color;
|
||||
|
||||
out vec4 v_color;
|
||||
out vec2 v_texCoord;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(a_position, 1.0f);
|
||||
v_color = a_color;
|
||||
v_texCoord = a_texCoord;
|
||||
}
|
||||
49
src/main.cpp
49
src/main.cpp
@@ -8,6 +8,8 @@
|
||||
#include "IndexBuffer.hpp"
|
||||
#include "VertexBuffer.hpp"
|
||||
#include "Shader.hpp"
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "../libs/stb_image.hpp"
|
||||
|
||||
void openGLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) {
|
||||
if (severity == GL_DEBUG_SEVERITY_NOTIFICATION) return; //ignore notifications (mostly performance information)
|
||||
@@ -100,11 +102,19 @@ int main() {
|
||||
#endif
|
||||
|
||||
Vertex vertices[] = {
|
||||
Vertex{-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f},
|
||||
Vertex{-0.5f, 0.5f, 0.0f, 0.0, 1.0f, 0.0f, 1.0f},
|
||||
Vertex{0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f},
|
||||
Vertex{-0.5f, -0.5f, 0.0f,
|
||||
0.0f, 0.0f,
|
||||
1.0f, 0.0f, 0.0f, 1.0f},
|
||||
Vertex{-0.5f, 0.5f, 0.0f,
|
||||
0.0f, 1.0f,
|
||||
0.0, 1.0f, 0.0f, 1.0f},
|
||||
Vertex{0.5f, -0.5f, 0.0f,
|
||||
1.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 1.0f},
|
||||
|
||||
Vertex{0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f},
|
||||
Vertex{0.5f, 0.5f, 0.0f,
|
||||
1.0f, 1.0f,
|
||||
1.0f, 0.0f, 0.0f, 1.0f},
|
||||
};
|
||||
uint32 numVertices = sizeof(vertices) / sizeof(Vertex);
|
||||
|
||||
@@ -119,6 +129,28 @@ int main() {
|
||||
VertexBuffer vertexBuffer(vertices, numVertices);
|
||||
vertexBuffer.unbind();
|
||||
|
||||
int32 textureWidth = 0;
|
||||
int32 textureHeight = 0;
|
||||
int32 bitsPerPixel = 0;
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
auto textureBuffer = stbi_load("../assets/logo.png", &textureWidth, &textureHeight, &bitsPerPixel, 4);
|
||||
|
||||
GLuint textureId;
|
||||
glGenTextures(1, &textureId);
|
||||
glBindTexture(GL_TEXTURE_2D, textureId);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // or GL_NEAREST for pixel like style (instead of GL_LINEAR -> interpolation)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureBuffer);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
if (textureBuffer) {
|
||||
stbi_image_free(textureBuffer);
|
||||
}
|
||||
|
||||
Shader shader("shaders/basic.vert", "shaders/basic.frag");
|
||||
shader.bind();
|
||||
|
||||
@@ -127,6 +159,11 @@ int main() {
|
||||
glUniform4f(colorUniformLocation, 1.0f, 0.0f, 1.0f, 1.0f);
|
||||
}
|
||||
|
||||
int textureUniformLocation = glGetUniformLocation(shader.getShaderId(), "u_texture");
|
||||
if (textureUniformLocation != 1) {
|
||||
glUniform1i(textureUniformLocation, 0);
|
||||
}
|
||||
|
||||
const uint64 perfCounterFrequency = SDL_GetPerformanceFrequency();
|
||||
uint64 lastCounter = SDL_GetPerformanceCounter();
|
||||
float32 delta = 0.0f;
|
||||
@@ -150,6 +187,8 @@ int main() {
|
||||
// FRAME GENERATION
|
||||
vertexBuffer.bind();
|
||||
indexBuffer.bind();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, textureId);
|
||||
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0);
|
||||
vertexBuffer.unbind();
|
||||
indexBuffer.unbind();
|
||||
@@ -170,6 +209,8 @@ int main() {
|
||||
lastCounter = endCounter;
|
||||
}
|
||||
|
||||
glDeleteTextures(1, &textureId);
|
||||
|
||||
SDL_GL_DestroyContext(context);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
|
||||
Reference in New Issue
Block a user