Skip to content

Instantly share code, notes, and snippets.

@erfg12
Created May 11, 2023 16:39
Show Gist options
  • Save erfg12/10a48b864d330d86f033ed505b67be30 to your computer and use it in GitHub Desktop.
Save erfg12/10a48b864d330d86f033ed505b67be30 to your computer and use it in GitHub Desktop.
[Episode 9] [Code] First OpenGL Triangle - Modern OpenGL - Mike Shah
#include <SDL.h>
#include <glad/glad.h>
#include <iostream>
#include <vector>
int gScreenWidth = 640;
int gScreenHeight = 480;
int quit = 0;
GLuint gVertexArrayObject = 0; // VAO
GLuint gVertexBufferObject = 0; // VBO
GLuint gGraphicsPipelineShaderProgram = 0; // store our shader object
// example shaders
const std::string gVertexShaderSource =
"#version 410 core\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position.x, position.y, position.z, position.w);\n"
"}\n";
const std::string gFragmentShaderSource =
"#version 410 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
" color = vec4(1.0f, 0.5f, 0.0, 1.0f);\n"
"}\n";
void PrintHWInfo() {
std::cout << glGetString(GL_VENDOR) << std::endl;
std::cout << glGetString(GL_RENDERER) << std::endl;
std::cout << glGetString(GL_VERSION) << std::endl;
std::cout << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
}
void VertexSpecification() {
// generate and bind VAO
const std::vector<GLfloat> vertexPosition{ // lives on CPU
// x y z
-0.8f, -0.8f, 0.0f, // vertex 1
0.8f, -0.8f, 0.0f, // vertex 2
0.0f, 0.8f, 0.0f // vertex 3
};
glGenVertexArrays(1, &gVertexArrayObject); // start sending to GPU
glBindVertexArray(gVertexArrayObject);
// generate and bind VBO
glGenBuffers(1, &gVertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, gVertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, vertexPosition.size() * sizeof(GLfloat), vertexPosition.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); // 3 points, float data, no rgba
glBindVertexArray(0);
glDisableVertexAttribArray(0);
}
GLuint CompileShader(GLuint type, const std::string& source) {
GLuint shaderObject;
if (type == GL_VERTEX_SHADER) {
shaderObject = glCreateShader(GL_VERTEX_SHADER);
}
else if (type == GL_FRAGMENT_SHADER) {
shaderObject = glCreateShader(GL_FRAGMENT_SHADER);
}
const char* src = source.c_str();
glShaderSource(shaderObject, 1, &src, nullptr);
glCompileShader(shaderObject);
return shaderObject;
}
GLuint CreateShaderProgram(const std::string& VertexShaderSource, const std::string& FragmentShaderSource) {
GLuint programObject = glCreateProgram(); // create graphics pipeline
GLuint myVertexShader = CompileShader(GL_VERTEX_SHADER, VertexShaderSource);
GLuint myFragmentShader = CompileShader(GL_FRAGMENT_SHADER, FragmentShaderSource);
glAttachShader(programObject, myVertexShader);
glAttachShader(programObject, myFragmentShader);
glLinkProgram(programObject);
return programObject;
}
void CreateGraphicsPipeline() {
gGraphicsPipelineShaderProgram = CreateShaderProgram(gVertexShaderSource, gFragmentShaderSource);
}
void PreDraw() {
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glViewport(0, 0, gScreenWidth, gScreenHeight);
glClearColor(1.f, 1.f, 0.1f, 1.f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glUseProgram(gGraphicsPipelineShaderProgram);
}
void Draw() {
glBindVertexArray(gVertexArrayObject);
glBindBuffer(GL_ARRAY_BUFFER, gVertexBufferObject);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
int main(int argc, char* args[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* win = SDL_CreateWindow("hello", 10, 50, 640, 480, SDL_WINDOW_OPENGL);
if (SDL_GL_CreateContext(win) == NULL) {
std::cout << "OpenGL context failed: " << SDL_GetError() << std::endl;
}
else {
if (!gladLoadGLLoader(SDL_GL_GetProcAddress)) {
std::cout << "glad was not initialized" << std::endl;
exit(1);
}
else {
PrintHWInfo();
VertexSpecification();
CreateGraphicsPipeline();
}
}
while (quit == 0) {
SDL_Event e;
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT)
quit = 1;
}
PreDraw();
Draw();
SDL_GL_SwapWindow(win);
}
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment