Jump to content
  • Advertisement
Sign in to follow this  
ukdeveloper

OpenGL UNRESOLVED: Rotation problem

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

Sorry for the third thread here today, I'm having real trouble [sad] I'm now trying to get a 2D shape to rotate, but it won't. Instead of there being rotation, the screen is filling in black from the right, and the black part slides across, almost like a blind is being pulled across horizontally. Here's my code, hope it helps:
#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};

GLfloat trirotate; // Rotation variable - stands for "triangle rotate"

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(1024,768,32,SDL_OPENGL|SDL_FULLSCREEN);

	glViewport(0,0,1024,768);
	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.0f);
	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();

		glColor3f(1.0f,0.25f,0.1f);

		glRecti(25,25,50,50); // This doesn't get "wiped", it's always on screen no matter what

		glTranslatef(200.0f,200.0f,0.0f);
		
		glLoadIdentity();
		
		glRotatef(trirotate,0.0f,1.0f,0.0f); // This should rotate the triangle, shouldn't it?  
If I change it to (1.0f,0.0f,0.0f), I get the same 
effect, except the "wipe" comes from the bottom of the screen except from the right.

		trirotate+=0.001f; /* Altering this affects the speed at which the "wipe" effect occurs - I've made it 
deliberately slow so that you can see easily see 
what's happening if you choose to run 
this, normally everything 
would be gone before 
you had a chance to look.*/

		glBegin(GL_TRIANGLES);

			glColor3f(1.0f,0.0f,0.0f); // Red
			glVertex3f(300.0f,100.0f,0.0f); // Bottom vertex of triangle

			glColor3f(0.0f,1.0f,0.0f); // Green
			glVertex3f(200.0f,200.0f,0.0f); // Top left of triangle

			glColor3f(0.0f,0.0f,1.0f); // Blue
			glVertex3f(400.0f,200.0f,0.0f); // Top right of triangle

		glEnd();

		

		glTranslatef(600.0f,200.0f,0.0f);

		//glLoadIdentity(); 
/* Uncommenting this and the glLoadIdentity(); further down causes everything to be drawn at the far left of the screen (x coordinate 0 it would seem)*/
		glBegin(GL_QUADS);

			glColor3f(1.0f,0.0f,0.0f); // Red
			glVertex3f(-100.0f,100.0f,0.0f); // Top left

			glColor3f(0.0f,1.0f,0.0f); // Green
			glVertex3f(100.0f,100.0f,0.0f); // Top right

			glColor3f(0.0f,0.0f,1.0f); // Blue
			glVertex3f(100.0f,-100.0f,0.0f); // Bottom left

			glColor3f(1.0f,1.0f,1.0f);
			glVertex3f(-100.0f,-100.0f,0.0f); // Bottom right

		glEnd();

		glTranslatef(25.0f,225.0f,0.0f);

		//glLoadIdentity(); // Same as above

		glBegin(GL_QUADS);

			glVertex3f(-100.0f,100.0f,0.0f); // Bottom left

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

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

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

		glEnd();

		SDL_GL_SwapBuffers();

		glFlush();

		

	}

	return success;

}









The code is based on that from NeHe lesson 4, and his example works perfectly. I could comment out the other two quads listed in the code below the triangle, but I don't think this makes a difference as when I do that the same effect is observed, just without the quads being on screen. I'm stuck. Sorry to create 3 new threads in a day here, but I don't have a clue. Once I get going I should begin to fly, maybe buy a book as I go. Thanks, ukdeveloper.
Please see new post down below. [Edited by - ukdeveloper on August 14, 2005 12:57:12 PM]

Share this post


Link to post
Share on other sites
Advertisement
it looks like your triangle is taking up the whole screen. try using smaller vertices, like:

glBegin(GL_TRIANGLES);
glColor3f(1.0f,1.0f,1.0f); //a white triangle
glVertex2f(0.0f,3.0f); //using glVertex2f because your z-coords are on 0, so no difference
glVertex2f(2.0f,0.0f);
glVertex2f(-2.0f,0.0f);
glEnd();


maybe that will work better. also, to get a better view of things, use the gluLookAt() function, this way you will be able to see more stuff. try using this code in your render() function:

gluLookAt(0.0f,0.0f,10.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f); //this postions the camera at (0,0,10)


that will probably help to. the reason you are getting a wipe effect is because your triangle is too large, so all you see is the middle of your triangle rotating.

maybe this will help you:-)

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!