Last active
May 1, 2016 07:31
-
-
Save Starl1ght/4a8ca7ee3ef644d3236d9c245b826893 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <iostream> | |
FILE _iob[] = { *stdin, *stdout, *stderr }; | |
extern "C" FILE * __cdecl __iob_func(void) { return _iob; } | |
#include <GL/glew.h> | |
#include <GL/wglew.h> | |
#include <SDL.h> | |
#pragma comment(lib, "legacy_stdio_definitions.lib") | |
#pragma comment(lib, "glew32.lib") | |
#pragma comment(lib, "opengl32.lib") | |
#pragma comment(lib, "SDL2.lib") | |
#pragma comment(lib, "SDL2main.lib") | |
int main(int argc, char* argv[]) { | |
SDL_Init(SDL_INIT_EVERYTHING); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); | |
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); | |
SDL_Window* win = SDL_CreateWindow("Goh", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); | |
SDL_GLContext context = SDL_GL_CreateContext(win); | |
glewExperimental = GL_TRUE; | |
if (glewInit() != GLEW_OK) { | |
throw 1; | |
}; | |
glViewport(0, 0, 800, 600); | |
GLfloat vertices[] = { | |
-0.5f, -0.5f, 0.0f, | |
0.5f, -0.5f, 0.0f, | |
0.0f, 0.5f, 0.0f | |
}; | |
GLuint VBO; | |
glGenBuffers(1, &VBO); | |
glBindBuffer(GL_ARRAY_BUFFER, VBO); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); | |
int quit = 0; | |
std::string sh1 { "#version 330 core\n layout(location = 0) in vec3 position;\n void main()\n { gl_Position = vec4(position.x, position.y, position.z, 1.0); }\n" }; | |
const char* csh1 = sh1.c_str(); | |
GLuint vertexShader; | |
vertexShader = glCreateShader(GL_VERTEX_SHADER); | |
glShaderSource(vertexShader, 1, &csh1, NULL); | |
glCompileShader(vertexShader); | |
std::string sh2{ "#version 330 core \n out vec4 color; \n void main() \n { color = vec4(1.0f, 0.5f, 0.2f, 1.0f); }\n" }; | |
const char* csh2 = sh2.c_str(); | |
GLuint fragmentShader; | |
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(fragmentShader, 1, &csh2, NULL); | |
glCompileShader(fragmentShader); | |
GLint success; | |
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); | |
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); | |
GLuint shaderProgram; | |
shaderProgram = glCreateProgram(); | |
glAttachShader(shaderProgram, vertexShader); | |
glAttachShader(shaderProgram, fragmentShader); | |
glLinkProgram(shaderProgram); | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); | |
glEnableVertexAttribArray(0); | |
// 0. Copy our vertices array in a buffer for OpenGL to use | |
glBindBuffer(GL_ARRAY_BUFFER, VBO); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); | |
// 1. Then set the vertex attributes pointers | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); | |
glEnableVertexAttribArray(0); | |
// 2. Use our shader program when we want to render an object | |
glUseProgram(shaderProgram); | |
// 3. Now draw the object | |
GLuint VAO; | |
glGenVertexArrays(1, &VAO); | |
// 2. Copy our vertices array in a buffer for OpenGL to use | |
glBindBuffer(GL_ARRAY_BUFFER, VBO); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); | |
// 3. Then set our vertex attributes pointers | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); | |
glEnableVertexAttribArray(0); | |
//4. Unbind the VAO | |
glBindVertexArray(0); | |
while (!quit) | |
{ | |
SDL_Event event; | |
while (SDL_PollEvent(&event)) | |
{ | |
/* If a quit event has been sent */ | |
if (event.type == SDL_QUIT) | |
{ | |
/* Quit the application */ | |
quit = 1; | |
} | |
} | |
glClear(GL_COLOR_BUFFER_BIT); | |
glClearColor(0.1f, 0.7f, 0.3f, 0); | |
glBindVertexArray(VAO); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
glBindVertexArray(0); | |
SDL_GL_SwapWindow(win); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment