korgan

Member
  • Content count

    2
  • Joined

  • Last visited

Community Reputation

103 Neutral

About korgan

  • Rank
    Newbie
  1. SDL 2 C++ OpenGL Problem

    Yes, I tryed some tutorials before. I post in opengl forums too. They solved it!.  I had to change only one line:   glUniformMatrix4fv(render_model_matrix_loc, 4, GL_FALSE, model_matrix); to: glUniformMatrix4fv(render_model_matrix_loc, 1, GL_FALSE, model_matrix); thx anyway!!
  2. SDL 2 C++ OpenGL Problem

    Im a begginer in opengl and Im learning about opengl 4.3. I am doing a reference guide book's example using SDL 2.0.  The only thing that i can see is a black window, i dont know what i should do. Anyone can help me? I tryied many things such as modifing frutum and translation. Any idea? Here is my code: This is Demo_OGL_2.h: #ifndef DEMO_OGL_II_H_ #define DEMO_OGL_II_H_ #include <iostream> #include <GL/gl3w.h> #include <GL/gl3.h> #include "SDL2/SDL.h" #include "LoadShaders.h" class Demo_OGL_2 { private: bool running; SDL_Window* window; SDL_GLContext ctxt; static const uint32_t WIN_HEIGHT = 768; //px static const uint32_t WIN_WIDTH = 1024; //px static const char* WIN_TITLE; //px /***************************************************/ /***************************************************/ /***************************************************/ float aspect; static const GLfloat cube_positions[]; static const GLfloat cube_colors[]; static const GLushort cube_indices[]; GLuint ebo[1]; GLuint vao[1]; GLuint vbo[1]; GLuint render_prog; GLuint render_model_matrix_loc; GLuint render_projection_matrix_loc; /* enum VAO_IDs { Triangles, NumVAOs }; enum Buffer_IDs { ArrayBuffer, NumBuffers }; enum Attrib_IDs { vPosition = 0, vColor = 1 }; GLuint VAOs[NumVAOs]; GLuint Buffers[NumBuffers]; static const GLuint NumVertices = 6;*/ public: Demo_OGL_2(); int Execute(){ return OnExecute(); } bool Init(){ return OnInit(); } void Loop(){ return OnLoop(); } void Render(){ return OnRender(); } void Cleanup(){ return OnCleanup(); } void Event(SDL_Event* Event){ OnEvent(Event); } int OnExecute(); bool OnInit(); void OnEvent(SDL_Event* Event); void OnLoop(); void OnRender(); void OnCleanup(); void setupOpenGl(); /***************************************************/ /***************************************************/ /***************************************************/ void exInit(); }; #endif /* DEMO_OGL_I_H_ */ And this is my Demo_OGL_2.cpp: #include <iostream> #include "Demo_OGL_2.h" #include "../../LibsNUtils/vmath.h" #include <GL/gl3w.h> #include <GL/gl3.h> #include "LoadShaders.h" //#define BUFFER_OFFSET(offset) ((void *) (offset)) const char* Demo_OGL_2::WIN_TITLE = "Titulo de la Ventana"; const GLfloat Demo_OGL_2::cube_positions[] = { -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }; const GLfloat Demo_OGL_2::cube_colors[] = { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f }; const GLushort Demo_OGL_2::cube_indices[] = { 0, 1, 2, 3, 6, 7, 4, 5, // First strip 0xFFFF, // <<-- This is the restart index 2, 6, 0, 4, 1, 5, 3, 7 // Second strip }; Demo_OGL_2::Demo_OGL_2() : running(false), window(NULL), ctxt(NULL), aspect(0), ebo(), vao(), vbo(), render_prog(0), render_model_matrix_loc(0), render_projection_matrix_loc(0){} void Demo_OGL_2::OnEvent(SDL_Event* event) { switch (event->type) { case SDL_KEYUP: switch(event->key.keysym.sym){ case SDLK_v: std::cout << glGetString(GL_VERSION) << std::endl; break; case SDLK_ESCAPE: running = false; break; default: break; } break; case SDL_QUIT: running = false; break; default: break; } } void Demo_OGL_2::OnLoop() {} void Demo_OGL_2::OnRender() { float t = float(GetTickCount() & 0x1FFF) / float(0x1FFF); //static float q = 0.0f; static const vmath::vec3 X(1.0f, 0.0f, 0.0f); static const vmath::vec3 Y(0.0f, 1.0f, 0.0f); static const vmath::vec3 Z(0.0f, 0.0f, 1.0f); glEnable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(render_prog); vmath::mat4 model_matrix(vmath::translate(0.0f, 0.0f, -5.0f) * vmath::rotate(t * 360.0f, Y) * vmath::rotate(t * 720.0f, Z)); vmath::mat4 projection_matrix(vmath::frustum(-1.0f, 1.0f, -aspect, aspect, 1.0f, 500.0f)); glUniformMatrix4fv(render_model_matrix_loc, 4, GL_FALSE, model_matrix); glUniformMatrix4fv(render_projection_matrix_loc, 1, GL_FALSE, projection_matrix); glBindVertexArray( vao[0] ); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[0]); #define USE_PRIMITIVE_RESTART 0 #if USE_PRIMITIVE_RESTART // When primitive restart is on, we can call one draw command glEnable(GL_PRIMITIVE_RESTART); glPrimitiveRestartIndex(0xFFFF); glDrawElements(GL_TRIANGLE_STRIP, 17, GL_UNSIGNED_SHORT, NULL); #else // Without primitive restart, we need to call two draw commands glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_SHORT, NULL); glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_SHORT, (const GLvoid *)(9 * sizeof(GLushort))); #endif SDL_GL_SwapWindow(window); } void Demo_OGL_2::OnCleanup() { glUseProgram(0); glDeleteProgram(render_prog); glDeleteVertexArrays(1, vao); glDeleteBuffers(1, vbo); SDL_GL_DeleteContext(ctxt); SDL_DestroyWindow(window); SDL_Quit(); } bool Demo_OGL_2::OnInit() { if(SDL_Init(SDL_INIT_EVERYTHING) < 0) return false; window = SDL_CreateWindow(WIN_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIN_WIDTH, WIN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); if(!window) return false; setupOpenGl(); exInit(); running = true; return true; } void Demo_OGL_2::setupOpenGl(){ //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32); ctxt = SDL_GL_CreateContext(window); //vsync ON SDL_GL_SetSwapInterval(1); if (gl3wInit()) { std::cout << "Error al Inicializar GL3W" << std::endl; } } void Demo_OGL_2::exInit(){ ShaderInfo shaders[] = { { GL_VERTEX_SHADER, "Resources/Demo_OGL_II/primitive_restart.vs.glsl" }, { GL_FRAGMENT_SHADER, "Resources/Demo_OGL_II/primitive_restart.fs.glsl" }, { GL_NONE, NULL } }; render_prog = LoadShaders( shaders ); glUseProgram( render_prog ); render_model_matrix_loc = glGetUniformLocation(render_prog, "model_matrix"); render_projection_matrix_loc = glGetUniformLocation(render_prog, "projection_matrix"); glGenBuffers(1, ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[0]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_indices), cube_indices, GL_STATIC_DRAW); glGenVertexArrays(1, vao); glBindVertexArray(vao[0]); glGenBuffers(1, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(cube_positions) + sizeof(cube_colors), NULL, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(cube_positions), cube_positions ); glBufferSubData(GL_ARRAY_BUFFER, sizeof(cube_positions), sizeof(cube_colors), cube_colors); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)sizeof(cube_positions)); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glViewport(0, 0, WIN_WIDTH, WIN_HEIGHT); aspect = float(WIN_HEIGHT) / float(WIN_WIDTH); } int Demo_OGL_2::OnExecute() { if (!Init()) return -1; SDL_Event event; while (running) { while (SDL_PollEvent(&event)) Event(&event); Loop(); Render(); } Cleanup(); return 0; } Vertex Shader: #version 330 uniform mat4 model_matrix; uniform mat4 projection_matrix; layout (location = 0) in vec4 position; layout (location = 1) in vec4 color; out vec4 vs_fs_color; void main(void) { vs_fs_color = color; gl_Position = projection_matrix * (model_matrix * position); } And the simplest fragment shader: #version 430 in vec4 vs_fs_color; layout (location = 0) out vec4 color; void main(void) { color = vs_fs_color; } if u want to try it, just do:    Demo_OGL_2 d;  return d.Execute();