you can now print images on the screen
This commit is contained in:
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