Jump to content
  • Advertisement
Sign in to follow this  
TTT_Dutch

OpenGL VBO not Drawing or Camera not set right

This topic is 2836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well I don't know what the problem with this code is, but I don't see a triangle on my screen..
Here is my main.cpp:



////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#define SFML_DYNAMIC
#include <GL/glew.h>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>
#include <string>


using namespace std;


class Win_Settings
{
public:
int width;
int height;
string title;
};

void initVBO(GLuint vbo, GLfloat vbodat);
void DrawVBO(GLuint vbo);


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
glewInit();


GLuint vbo;
GLfloat vbodat[12] = { -5.0f, 0.0f,-5.0f,
0.0f, 5.0f, -5.0f,
5.0f, 0.0f, -5.0f};


Win_Settings win_dat;
win_dat.width = 600; win_dat.height = 600; win_dat.title = "VBO Test Area";

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (float)win_dat.width/(float)win_dat.height, 0, 500);


glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*9, &vbodat[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, NULL);

// Create the main window
sf::RenderWindow window(sf::VideoMode(win_dat.width, win_dat.height), win_dat.title);


// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(1.f);


// Create a clock for measuring the time elapsed
sf::Clock clock;


// Start game loop
while (window.IsOpened())
{

window.ShowMouseCursor(true); //Shows the cursor.

// Process events
sf::Event event; //Creates an event variable
while (window.GetEvent(event))
{

// Close window : exit
if (event.Type == sf::Event::Closed || (event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Key::Escape))
{
window.Close();
return 0;
}

// Adjust the viewport when the window is resized
if (event.Type == sf::Event::Resized)
{
glViewport(0, 0, event.Size.Width, event.Size.Height);
}
}

window.Clear();

window.SetActive();

// Clear the depth buffer
glClearColor(0.f,0.f,0.f,0.f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

glBindBuffer(GL_ARRAY_BUFFER, vbo);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, NULL);

window.Display();

}
return EXIT_SUCCESS;
}

Share this post


Link to post
Share on other sites
Advertisement
I have no experience with SFML but from what I can see you don't have a GL context when you call your matrix and buffer functions. Try moving all GL calls below the window creation where I assume a GL context is created.

Share this post


Link to post
Share on other sites

I have no experience with SFML but from what I can see you don't have a GL context when you call your matrix and buffer functions. Try moving all GL calls below the window creation where I assume a GL context is created.


Thanks for the reply! I finally got it working but now I have a new problem. I cant rotate my vbo.. Here is my code.



////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#define SFML_DYNAMIC
#include <GL/glew.h>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>
#include <string>
#include "Camera.h"


using namespace std;


class Win_Settings
{
public:
int width;
int height;
string title;
};


GLuint vbo;
GLfloat vbodat[12] = { -5.0f, 0.0f,-5.0f,
0.0f, 5.0f, -5.0f,
5.0f, 0.0f, -5.0f};

void initVBO();
void DrawVBO();


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
glewInit();


Camera cam1;
cam1.Setposition(0,0,-50);



Win_Settings win_dat;
win_dat.width = 600; win_dat.height = 600; win_dat.title = "VBO Test Area";

initVBO();

// Create the main window
sf::RenderWindow window(sf::VideoMode(win_dat.width, win_dat.height), win_dat.title);


// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(1.f);


// Create a clock for measuring the time elapsed
sf::Clock clock;


// Start game loop
while (window.IsOpened())
{
cam1.Update();
window.ShowMouseCursor(true); //Shows the cursor.

// Process events
sf::Event event; //Creates an event variable
while (window.GetEvent(event))
{

// Close window : exit
if (event.Type == sf::Event::Closed || (event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Key::Escape))
{
window.Close();
return 0;
}

// Adjust the viewport when the window is resized
if (event.Type == sf::Event::Resized)
{
glViewport(0, 0, event.Size.Width, event.Size.Height);
}
if (event.Type == sf::Event::KeyPressed)
{
switch( event.Key.Code)
{
case sf::Key::W:
cam1.Move(0,0,1);
break;
case sf::Key::S:
cam1.Move(0,0,-1);
break;
case sf::Key::D:
cam1.Move(-1,0,0);
break;
case sf::Key::A:
cam1.Move(1,0,0);
break;
}
}
}

window.Clear();

window.SetActive();

// Clear the depth buffer
glClearColor(0.f,0.f,0.f,0.f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glRotatef(180,0,1,0);
DrawVBO();
glPopMatrix();

window.Display();

}
return EXIT_SUCCESS;
}
void initVBO()
{

glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*9, &vbodat[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, NULL);
}
void DrawVBO()
{
glBindBuffer(GL_ARRAY_BUFFER, vbo);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, NULL);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!