Jump to content
  • Advertisement
Sign in to follow this  
ukdeveloper

OpenGL New to OpenGL, problem :(

This topic is 4842 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

Hi, I'm having a severe problem with a simple little thing in OpenGL. Here's the code I'm using:
#include <SDL/SDL_opengl.h>
#include <string>
#include <GL/GL.h>
#include <GL/GLU.h>

enum RETURNTYPE {success,fail};

int main(int argc, char *argv[])
{

	std::string GameName="SDL OpenGL sample";

	if(SDL_Init(SDL_INIT_VIDEO)<0)
	{

		MessageBox(NULL,L"SDL Initialisation failed.",L"",MB_OK|MB_ICONSTOP);

		return fail;

	}



	SDL_GL_SetAttribute(SDL_GL_RED_SIZE,5);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,5);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,5);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,32);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);

	SDL_Surface *screen=SDL_SetVideoMode(800,600,32,SDL_OPENGLBLIT);

	glViewport(0,0,800,600);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0f,800,600,0.0f,-1.0f,1.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glClearColor(0.0f,0.0f,0.0f,0.5f);
	glClearDepth(1.0f);
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_DEPTH_TEST);
	glShadeModel(GL_SMOOTH);
	glDisable(GL_CULL_FACE);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);

	
	
	
	while(1)
	{

		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

		glLoadIdentity();

		glTranslatef(-1.5f,0.0f,-6.0f);

		glBegin(GL_TRIANGLES);

			//glColor3f(1.0f,0.0f,0.0f);
			glVertex3f(0.0f,1.0f,0.0f);

			//glColor3f(0.0f,1.0f,0.0f);
			glVertex3f(-1.0f,-1.0f,0.0f);

			//glColor3f(0.0f,0.0f,1.0f);
			glVertex3f(1.0f,-1.0f,0.0f);

		glEnd();
		
		//glFlush();

		glRecti(25,25,50,50);

		SDL_GL_SwapBuffers();
	}

	return success;

}



The problem I'm having is that the glBegin isn't working at all, for any shape I try to make. And if I leave it, and call glRect afterwards, the product of that doesn't appear either. If I comment out glTranslatef(), and the glBegin -> glEnd subroutine, glRecti() works fine. glBegin isn't working at all, no matter what I try - the commented out sections are me juggling things around in an effort to make it work. Can anybody help? thanks, ukdeveloper. [Edited by - ukdeveloper on August 13, 2005 2:07:07 PM]

Share this post


Link to post
Share on other sites
Advertisement
I don't have SDL so I can't do a test to be sure, but it looks like your glTranslate is pushing your stuff outside the frustum.

glOrtho(0.0f,800,600,0.0f,-1.0f,1.0f);
This is setting up your frustum to go from -1 to 1 in the Z direction.

glTranslatef(-1.5f,0.0f,-6.0f);
This translates to -6 in Z. That is outside of the -1 to 1 range you used.

I suspect you intended to do glTranslatef(-1.5f,-6.0f,0.0f); instead.

Share this post


Link to post
Share on other sites
I tried changing those values like you suggested, but still nothing.

Here's the code as it stands now:


#include <iostream>
#include <cmath>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <string>
#include <GL/GL.h>
#include <GL/GLU.h>

enum RETURNTYPE {success,fail};

int main(int argc, char *argv[])
{

std::string GameName="SDL OpenGL sample";

if(SDL_Init(SDL_INIT_VIDEO)<0)
{

MessageBox(NULL,L"SDL Initialisation failed.",L"",MB_OK|MB_ICONSTOP);

return fail;

}


SDL_GL_SetAttribute(SDL_GL_RED_SIZE,5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);

SDL_Surface *screen=SDL_SetVideoMode(800,600,32,SDL_OPENGLBLIT);

glViewport(0,0,800,600);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f,800,600,0.0f,-1.0f,1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glClearColor(0.0f,0.0f,0.0f,0.5f);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glDisable(GL_CULL_FACE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);




while(1)
{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

glTranslatef(0.0f,0.0f,0.0f);

glBegin(GL_TRIANGLES);

glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);

glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);

glColor3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,-1.0f,0.0f);

glEnd();

glFlush();

glRecti(25,25,50,50);

SDL_GL_SwapBuffers();
}

return success;

}




Now, at least, with the triangle subroutine not commented out, glRecti() works and the rectangle is filled in blue due to me not setting another colour after the final triangle vertex.

But the triangle still won't appear at all, have I got glOrtho() mixed up or something?

Share this post


Link to post
Share on other sites
I put your code into my GL framework, and it's fine. The only problem is that your triangle is very very small, and partially off screen. Remember that you set your frustum to be 0-800 in X and 0-600 in Y. That means the center of your screen is at (400, 300), but you are drawing your triangle centered around (0, 0).

Try these vertices out:
<code>
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(100.0f,100.0f,0.0f);

glColor3f(0.0f,1.0f,0.0f);
glVertex3f(700.0f,300.0f,0.0f);

glColor3f(0.0f,0.0f,1.0f);
glVertex3f(400.0f,500.0f,0.0f);
</code>

Share this post


Link to post
Share on other sites
[EDIT] "I'm too old for this ****." [/EDIT]

The only problem is that the triangle is too small to be seen, i think ;)

Try,

glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,100.0f,0.0f);

glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-100.0f,-100.0f,0.0f);

glColor3f(0.0f,0.0f,1.0f);
glVertex3f(100.0f,-100.0f,0.0f);

but you still wont see the whole triangle since most of it is outside the screen.
Try translating it down and to the right a bit,

glTranslatef(200.0f, 200.0f, 0);

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!