factor out init functions
This commit is contained in:
parent
9e004cdb5b
commit
5082b4abc7
76
src/gfx.c
76
src/gfx.c
|
@ -1,8 +1,22 @@
|
||||||
#include "gfx.h"
|
#include "gfx.h"
|
||||||
|
#include "stb_image.h"
|
||||||
|
|
||||||
const char* vertShaderPath = "res/shader/shader.vert";
|
const char* vertShaderPath = "res/shader/shader.vert";
|
||||||
const char* fragShaderPath = "res/shader/shader.frag";
|
const char* fragShaderPath = "res/shader/shader.frag";
|
||||||
|
|
||||||
|
float vertices[] = {
|
||||||
|
// position color uvs
|
||||||
|
1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
|
||||||
|
1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
|
||||||
|
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
|
||||||
|
-1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
|
||||||
|
};
|
||||||
|
|
||||||
|
unsigned int indices[] = {
|
||||||
|
0, 1, 3,
|
||||||
|
1, 2, 3
|
||||||
|
};
|
||||||
|
|
||||||
SDL_Window* sdlWindow;
|
SDL_Window* sdlWindow;
|
||||||
SDL_GLContext* sdlContext;
|
SDL_GLContext* sdlContext;
|
||||||
|
|
||||||
|
@ -99,3 +113,65 @@ GLuint compileShader(const char* path, GLenum type)
|
||||||
|
|
||||||
return shader;
|
return shader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setVertexAttributes()
|
||||||
|
{
|
||||||
|
int stride = 8*sizeof(float);
|
||||||
|
|
||||||
|
// position
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
// color
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void*)(3*sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
// uv
|
||||||
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void*)(6*sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void createTextureFromFile()
|
||||||
|
{
|
||||||
|
// create an opengl texture and bind it to the GL_TEXTURE_2D target
|
||||||
|
unsigned int texture;
|
||||||
|
glGenTextures(1, &texture);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, texture);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_REPEAT);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_REPEAT);
|
||||||
|
|
||||||
|
// load image data from file
|
||||||
|
const char* imagePath = "res/tex.png";
|
||||||
|
int width, height, nrChannels;
|
||||||
|
stbi_set_flip_vertically_on_load(1);
|
||||||
|
unsigned char* data = stbi_load(imagePath, &width, &height, &nrChannels, 0);
|
||||||
|
if (!data)
|
||||||
|
{
|
||||||
|
fputs("failed to load texture data: ", stderr);
|
||||||
|
fputs(imagePath, stderr);
|
||||||
|
fputs("\n", stderr);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// read image data into bound opengl texture
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
|
||||||
|
stbi_image_free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void initBuffers()
|
||||||
|
{
|
||||||
|
unsigned int VAO, VBO, EBO;
|
||||||
|
glGenVertexArrays(1, &VAO); // vertex array object
|
||||||
|
glGenBuffers(1, &VBO); // vertex buffer object
|
||||||
|
glGenBuffers(1, &EBO); // element buffer object
|
||||||
|
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||||
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||||
|
}
|
||||||
|
|
|
@ -13,3 +13,7 @@ SDL_Window* gfxInit();
|
||||||
|
|
||||||
unsigned int compileShaderProgram();
|
unsigned int compileShaderProgram();
|
||||||
|
|
||||||
|
void createTextureFromFile();
|
||||||
|
void setVertexAttributes();
|
||||||
|
void initBuffers();
|
||||||
|
|
||||||
|
|
81
src/main.c
81
src/main.c
|
@ -1,20 +1,6 @@
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
|
||||||
#include "gfx.h"
|
#include "gfx.h"
|
||||||
#include "stb_image.h"
|
|
||||||
|
|
||||||
float vertices[] = {
|
|
||||||
// position color uvs
|
|
||||||
1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
|
|
||||||
1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
|
|
||||||
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
|
|
||||||
-1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
|
|
||||||
};
|
|
||||||
|
|
||||||
unsigned int indices[] = {
|
|
||||||
0, 1, 3,
|
|
||||||
1, 2, 3
|
|
||||||
};
|
|
||||||
|
|
||||||
// forward declarations
|
// forward declarations
|
||||||
|
|
||||||
|
@ -28,68 +14,13 @@ int main()
|
||||||
{
|
{
|
||||||
SDL_Window* window = gfxInit();
|
SDL_Window* window = gfxInit();
|
||||||
|
|
||||||
// generate opengl texture
|
createTextureFromFile();
|
||||||
unsigned int texture;
|
|
||||||
glGenTextures(1, &texture);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, texture);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
||||||
|
|
||||||
// load image data from file
|
initBuffers();
|
||||||
const char* imageFile = "res/tex.png";
|
|
||||||
int width, height, nrChannels;
|
|
||||||
stbi_set_flip_vertically_on_load(1);
|
|
||||||
unsigned char* data = stbi_load(imageFile, &width, &height, &nrChannels, 0);
|
|
||||||
if (!data)
|
|
||||||
{
|
|
||||||
fputs("failed to load texture data: ", stderr);
|
|
||||||
fputs(imageFile, stderr);
|
|
||||||
fputs("\n", stderr);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
|
||||||
|
|
||||||
// free loaded image data
|
setVertexAttributes();
|
||||||
stbi_image_free(data);
|
|
||||||
|
|
||||||
unsigned int shaderProgram = compileShaderProgram();
|
unsigned int shaderProgram = compileShaderProgram();
|
||||||
|
|
||||||
// vertex array object
|
|
||||||
unsigned int VAO;
|
|
||||||
glGenVertexArrays(1, &VAO);
|
|
||||||
// vertex buffer object
|
|
||||||
unsigned int VBO;
|
|
||||||
glGenBuffers(1, &VBO);
|
|
||||||
// element buffer object
|
|
||||||
unsigned int EBO;
|
|
||||||
glGenBuffers(1, &EBO);
|
|
||||||
|
|
||||||
glBindVertexArray(VAO);
|
|
||||||
|
|
||||||
// copy our vertices into a buffer opengl can use
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO); // bind it to the GL_ARRAY_BUFFER target
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // copy user-defined data into currently bound buffer
|
|
||||||
|
|
||||||
// copy indices into a buffer for opengl
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
// set vertex attributes
|
|
||||||
|
|
||||||
int stride = 8*sizeof(float);
|
|
||||||
// position attribute
|
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0); // TODO: wtf
|
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
// color attribute
|
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float)));
|
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
// uv attribute
|
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void*)(6 * sizeof(float)));
|
|
||||||
glEnableVertexAttribArray(2);
|
|
||||||
|
|
||||||
glUseProgram(shaderProgram);
|
glUseProgram(shaderProgram);
|
||||||
|
|
||||||
// render loop
|
// render loop
|
||||||
|
@ -99,12 +30,6 @@ int main()
|
||||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
// update uniforms
|
|
||||||
//float time = (float)SDL_GetTicks() / 1000.0f;
|
|
||||||
//float greenValue = (sin(time)/2.0f)+0.5f;
|
|
||||||
//int vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor");
|
|
||||||
//glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
|
|
||||||
|
|
||||||
// draw
|
// draw
|
||||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||||
SDL_GL_SwapWindow(window);
|
SDL_GL_SwapWindow(window);
|
||||||
|
|
Loading…
Reference in New Issue