Page 1 of 1

SDL OpenGL

Posted: Sun Mar 07, 2010 4:39 pm
by eatcomics
I'm making an OpenGL SDL app, and it compiles and runs. The screen clears to white like I want, but it doesn't draw the triangle and the program stops responding... I don't have any clue what would be wrong, I'm a noob to this so any help what so ever is appreciated!

Includes.h

Code: Select all

#include <stdio.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include "Constants.h"
Constants.h

Code: Select all

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
SDLinit.h just holds the prototype for the function
SDLinit.cpp

Code: Select all

#include "Includes.h"
#include "OGLinit.h"
#include "SDLinit.h"

//Sets up window and stuff
bool initVideo()
{
	//init SDL
	if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
	{
		//if init didn't go well, report the error and return false
		printf("SDLinit Fail: %s\n", SDL_GetError());
		return false;
	}
	//if you exit, quit SDL
	atexit(SDL_Quit);

	//get some info about the player's computer
	const SDL_VideoInfo *info = SDL_GetVideoInfo();
	if (!info)
	{
		//I don't really need this, it shouldn't happen, but ya never know
		printf("Video Query Failed: %s\n", SDL_GetError());
		return false;
	}
	
	//get bpp
	int bpp = info->vfmt->BitsPerPixel;

	//Set up bits for color and some other stuff
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

	if (SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, bpp, SDL_OPENGL | SDL_SWSURFACE) == 0)
	{
		printf("VideoMode Fail: %s\n", SDL_GetError());
		return false;
	}

	//it all worked, now return oglsetup
	return OGLSetup(SCREEN_WIDTH, SCREEN_HEIGHT);
}
OGLinit.h contains prototype
OGLinit.cpp

Code: Select all

#include "Includes.h"
#include "SDLinit.h"
#include "OGLinit.h"

//does opengl stuff
bool OGLSetup(int width, int height)
{
	//enable ogl stuff
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_COLOR_MATERIAL);
    //clear the screen with a nice white color
	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
	//set viewport to the screen width and height
	glViewport(0, 0, width, height);
	//start setting the camera perspective
	glMatrixMode(GL_PROJECTION);
	//reset the camera
	glLoadIdentity();
	//set perspective
	gluPerspective(45.0, (double) width / (double) height, 1.0, 200.0);
	
	return true;
}
DrawStuff.h holds prototype
DrawStuff.cpp

Code: Select all

#include "Includes.h"

void Draw()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);

	glLoadIdentity();
	//glPushMatrix();
	glColor3f(0.0f, 0.0f, 1.0f);

	glBegin(GL_TRIANGLES);
	    glVertex3f(0.0f, 0.0f, 4.0f);
		glVertex3f(1.0f, 0.0f, 4.0f);
		glVertex3f(1.0f, 1.0f, 4.0f);
	glEnd();

	//glPopMatrix();

	SDL_GL_SwapBuffers();
}
Main.cpp

Code: Select all

#include "Includes.h"
#include "SDLinit.h"
#include "OGLinit.h"
#include "DrawStuff.h"

int main(int argc, char *arv[])
{
	if (!initVideo())
	{
		printf("SDL/OGL Setup Fail");
		return 1;
	}
	SDL_Event event;
	bool done = false;
	while (!done)
	{
		if (event.type == SDL_KEYDOWN)
		{
			done = true;
		}
		Draw();
	}
	return 0;
}
:cry:

Re: SDL OpenGL

Posted: Sun Mar 07, 2010 4:45 pm
by Bakkon
You need to be polling for SDL events.

Re: SDL OpenGL

Posted: Sun Mar 07, 2010 4:51 pm
by eatcomics
Bakkon wrote:You need to be polling for SDL events.
Thank you very much, can't believe I forgot that

Its still not drawing the figure though... anyone know why that's not working?

Re: SDL OpenGL

Posted: Sun Mar 07, 2010 5:10 pm
by GroundUpEngine
Change to these ;)

Code: Select all

//does opengl stuff
bool OGLSetup(int width, int height)
{
    // Set color and depth clear value
    glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 0.f);

    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);

    // Setup a perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.f, 1.f, 1.f, 500.f);
   
   return true;
}

Code: Select all

void Draw()
{
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glTranslatef(0.f, 0.f, -200.f);
   glBegin(GL_QUADS);
       glVertex3f(-50.f, -50.f, -50.f);
       glVertex3f(-50.f,  50.f, -50.f);
       glVertex3f( 50.f,  50.f, -50.f);
       glVertex3f( 50.f, -50.f, -50.f);
   glEnd();

   SDL_GL_SwapBuffers();
}

Code: Select all

int main(int argc, char *arv[])
{
   if(!initVideo())
   {
      printf("SDL/OGL Setup Fail");
      return 1;
   }
   SDL_Event event;
   bool done = false;
   
   while(!done)
   {
		while( SDL_PollEvent( &event ) )
		{
			if( event.type == SDL_QUIT ) done = true;
		}
      Draw();
   }
   return 0;
}

Re: SDL OpenGL

Posted: Sun Mar 07, 2010 6:23 pm
by eatcomics
Thanks! that works, but can you please explain to me why it worked? Sorry, I guess I'm just missing something here

Edit: Was it because I wasn't setting up the perspective right, or that I didn't clear the depth buffer, or like I was drawing the triangle in the wrong place?

Re: SDL OpenGL

Posted: Sun Mar 07, 2010 6:46 pm
by GroundUpEngine
eatcomics wrote:Thanks! that works, but can you please explain to me why it worked? Sorry, I guess I'm just missing something here

Edit: Was it because I wasn't setting up the perspective right, or that I didn't clear the depth buffer, or like I was drawing the triangle in the wrong place?
Nah just setting up OpenGL 8-)

Re: SDL OpenGL

Posted: Sun Mar 07, 2010 7:02 pm
by eatcomics
I see, thanks :D