• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
korgan

SDL 2 C++ OpenGL Problem

2 posts in this topic

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();
 
0

Share this post


Link to post
Share on other sites
That's a lot of moving parts. Have you managed to draw anything before, like with a previous tutorial? Anything at all?

First the low-hanging fruit: I'd check for GL errors at the end of the render loop, check that there were no errors compiling and linking the shaders, and I'd turn face culling off just in case.

Then I'd try to draw *something* on the screen, like a single triangle or a couple of points, as fast as possible. Strip out and simplify large chunks of code until you see something. Then you can start re-adding the stuff piece by piece and see which of them was the problem.

Assuming your GL is in compatibility mode, you can initially disable the shader program and just draw your primitive with glBegin, glVertex etc. If that works, try to draw with the simplest possible shaders, like a vertex shader that ignores input entirely and outputs the positions of a single fixed triangle based on gl_VertexID, and a fragment shader that outputs constant white. Then add the position input back in, add indexed drawing, etc.
0

Share this post


Link to post
Share on other sites

Yes, I tryed some tutorials before. I post in opengl forums too. They solved it!. 

I had to change only one line:

 

  1. glUniformMatrix4fv(render_model_matrix_loc, 4, GL_FALSE, model_matrix);
  2. to:

    glUniformMatrix4fv(render_model_matrix_loc, 1, GL_FALSE, model_matrix);

thx anyway!!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0