Created
July 16, 2017 02:40
-
-
Save msmshazan/08c1c4ff96e3246d58bfbb170277c2b8 to your computer and use it in GitHub Desktop.
Opengl not working example
This file contains hidden or 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 <SDL.h> | |
#include "glad.c" | |
int main(int argc,char **argv){ | |
SDL_Init(SDL_INIT_EVERYTHING); | |
SDL_DisplayMode CurrentDisplay; | |
SDL_GetCurrentDisplayMode(0, &CurrentDisplay); | |
SDL_Window *Window = SDL_CreateWindow("Game SDL",10,10,800,600,SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN); | |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); | |
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); | |
SDL_GL_SetSwapInterval(-1); | |
SDL_GLContext glcontext = SDL_GL_CreateContext(Window); | |
SDL_GL_LoadLibrary(NULL); | |
gladLoadGLLoader(SDL_GL_GetProcAddress); | |
printf("Vendor: %s\n", glGetString(GL_VENDOR)); | |
printf("Renderer: %s\n", glGetString(GL_RENDERER)); | |
printf("Version: %s\n", glGetString(GL_VERSION)); | |
SDL_Event event; | |
GLuint VertexArrayID; | |
glGenVertexArrays(1, &VertexArrayID); | |
glBindVertexArray(VertexArrayID); | |
static GLfloat VAO_data[] = { | |
-1.0f, -1.0f, 0.0f, | |
1.0f, -1.0f, 0.0f, | |
0.0f, 1.0f, 0.0f, | |
}; | |
// This will identify our vertex buffer | |
GLuint vertexbuffer; | |
// Generate 1 buffer, put the resulting identifier in vertexbuffer | |
glGenBuffers(1, &vertexbuffer); | |
// The following commands will talk about our 'vertexbuffer' buffer | |
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); | |
// Give our vertices to OpenGL. | |
glBufferData(GL_ARRAY_BUFFER, sizeof(VAO_data), VAO_data, GL_STATIC_DRAW); | |
bool running = true; | |
while(running){ | |
while(SDL_PollEvent(&event)){ | |
//handle event | |
if(event.type == SDL_QUIT) running = false; | |
} | |
glClear(GL_COLOR_BUFFER_BIT); | |
glClearColor(1,1,0,1); | |
glClear (GL_COLOR_BUFFER_BIT); // Clear the window | |
glLoadIdentity (); | |
glEnableVertexAttribArray(0); | |
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); | |
glVertexAttribPointer( | |
0, // attribute 0. No particular reason for 0, but must match the layout in the shader. | |
3, // size | |
GL_FLOAT, // type | |
GL_FALSE, // normalized? | |
0, // stride | |
(void*)0 // array buffer offset | |
); | |
// Draw the triangle ! | |
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle | |
glDisableVertexAttribArray(0); | |
// Create the shaders | |
GLint Result = GL_FALSE; | |
int InfoLogLength; | |
char * VertexSource = "#version 120 | |
layout(location = 0) in vec3 vertexPosition_modelspace; | |
void main(){ | |
gl_Position.xyz = vertexPosition_modelspace; | |
gl_Position.w = 1.0; | |
} | |
"; | |
char * FragmentSource = "#version 120 | |
out vec3 color; | |
void main(){ | |
color = vec3(1,0,0); | |
}"; | |
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); | |
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); | |
//Compile Shaders | |
glShaderSource(VertexShaderID, 1, &VertexSource , NULL); | |
glCompileShader(VertexShaderID); | |
// Check Vertex Shader | |
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result); | |
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); | |
// Compile Fragment Shader | |
glShaderSource(FragmentShaderID, 1, &FragmentSource , NULL); | |
glCompileShader(FragmentShaderID); | |
// Check Fragment Shader | |
glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result); | |
glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); | |
// Link the program | |
printf("Linking program\n"); | |
GLuint ProgramID = glCreateProgram(); | |
glAttachShader(ProgramID, VertexShaderID); | |
glAttachShader(ProgramID, FragmentShaderID); | |
glLinkProgram(ProgramID); | |
glDetachShader(ProgramID, VertexShaderID); | |
glDetachShader(ProgramID, FragmentShaderID); | |
glDeleteShader(VertexShaderID); | |
glDeleteShader(FragmentShaderID); | |
SDL_GL_SwapWindow(Window); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment