Jump to content

  • Log In with Google      Sign In   
  • Create Account


I need help drawing a square using SDL/Opengl.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 04 October 2012 - 07:49 AM

[source lang="cpp"]/*This source code copyrighted by Lazy Foo' Productions (2004-2012)and may not be redistributed without written permission.*///The headers#include "SDL.h"#include <SDL_opengl.h>#pragma comment(lib, "opengl32.lib")#pragma comment(lib, "glu32.lib")bool initGL();bool init();void handleKeys( unsigned char, int , int);void update();void render();void clean_up();//Screen attributesconst int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;const int SCREEN_BPP = 32;//The frame rateconst int FRAMES_PER_SECOND = 60;//Event handlerSDL_Event event;//Rendering flagbool renderQuad = true;//The timerclass Timer{ private: //The clock time when the timer started int startTicks; //The ticks stored when the timer was paused int pausedTicks; //The timer status bool paused; bool started; public: //Initializes variables Timer(); //The various clock actions void start(); void stop(); void pause(); void unpause(); //Gets the timer's time int get_ticks(); //Checks the status of the timer bool is_started(); bool is_paused();};bool initGL(){ //Initialize Projection Matrix glMatrixMode( GL_PROJECTION ); glLoadIdentity(); //Initialize Modelview Matrix glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); //Initialize clear color glClearColor( 0.f, 0.f, 0.f, 1.f ); //Check for error GLenum error = glGetError(); if( error != GL_NO_ERROR ) { printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) ); return false; } return true;}bool init(){ //Initialize SDL if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) { return false; } //Create Window if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL ) { return false; } //Enable unicode SDL_EnableUNICODE( SDL_TRUE ); //Initialize OpenGL if( initGL() == false ) { return false; } //Set caption SDL_WM_SetCaption( "OpenGL Test", NULL ); return true;}void handleKeys( unsigned char key, int x, int y ){ //Toggle quad if( key == 'q' ) { renderQuad = !renderQuad; }}void update(){}void render(){ //Clear color buffer glClear( GL_COLOR_BUFFER_BIT ); //Render quad if( renderQuad == true ) { glBegin( GL_QUADS ); glVertex2f( -0.5f, -0.5f ); glVertex2f( 0.5f, -0.5f ); glVertex2f( 0.5f, 0.5f ); glVertex2f( -0.5f, 0.5f ); glEnd(); } //Update screen SDL_GL_SwapBuffers();}void clean_up(){ //Quit SDL SDL_Quit();}Timer::Timer(){ //Initialize the variables startTicks = 0; pausedTicks = 0; paused = false; started = false;}void Timer::start(){ //Start the timer started = true; //Unpause the timer paused = false; //Get the current clock time startTicks = SDL_GetTicks();}void Timer::stop(){ //Stop the timer started = false; //Unpause the timer paused = false;}void Timer::pause(){ //If the timer is running and isn't already paused if( ( started == true ) && ( paused == false ) ) { //Pause the timer paused = true; //Calculate the paused ticks pausedTicks = SDL_GetTicks() - startTicks; }}void Timer::unpause(){ //If the timer is paused if( paused == true ) { //Unpause the timer paused = false; //Reset the starting ticks startTicks = SDL_GetTicks() - pausedTicks; //Reset the paused ticks pausedTicks = 0; }}int Timer::get_ticks(){ //If the timer is running if( started == true ) { //If the timer is paused if( paused == true ) { //Return the number of ticks when the timer was paused return pausedTicks; } else { //Return the current time minus the start time return SDL_GetTicks() - startTicks; } } //If the timer isn't running return 0;}bool Timer::is_started(){ return started;}bool Timer::is_paused(){ return paused;}int main( int argc, char *argv[] ){ //Quit flag bool quit = false; //Initialize if( init() == false ) { return 1; } //The frame rate regulator Timer fps; //Wait for user exit while( quit == false ) { //Start the frame timer fps.start(); //While there are events to handle while( SDL_PollEvent( &event ) ) { if( event.type == SDL_QUIT ) { quit = true; } else if( event.type == SDL_KEYDOWN ) { //Handle keypress with current mouse position int x = 0, y = 0; SDL_GetMouseState( &x, &y ); handleKeys( event.key.keysym.unicode, x, y ); } } //Run frame update update(); //Render frame render(); //Cap the frame rate if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) { SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() ); } } //Clean up clean_up(); return 0;}[/source]

Ok, ignore the timer code, my problem is (that I cant seem to find a solution to after days of searching) is the drawing of the square.

You see how its a 0.5x0.5 square? Well its 0.5 of the screen. Run this code on your computer and see. IDK why its like this. If the screen size is 640x480 then shouldnt the square be very small on the screen? I dont want it to be 0.5 of the screens width/height but just 0.5 units long.


I've used freeglut and could draw squares fine without any problems. Now that I'm using SDL/Opengl together I have this problem.

When I change the screen size it changes the entire window size. When I draw a square thats 1.0x1.0 for example its not drawn as 1/the screens size, but as 1 = a whole. So the square thats 1x1 is automatically the entire screen regardless of its size and a square 0.5x0.5 is automatically half the screens size. I need to fix this.

Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7923

Like
0Likes
Like

Posted 04 October 2012 - 08:01 AM

The modelview and projection matrices are both identity matrices, which means the visible coordinate range is from -1 to 1 along all axes. If you draw a quad that extends from -0.5 to 0.5, then, on a coordinate system that is from -1 to 1, you'll get a quad that is half the size of the screen.

If you want another coordinate system, then you have to set up another coordinate system as well. Look up glOrtho to begin with.

Edited by Brother Bob, 04 October 2012 - 08:02 AM.


#3 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 04 October 2012 - 08:08 AM

The modelview and projection matrices are both identity matrices, which means the visible coordinate range is from -1 to 1 along all axes. If you draw a quad that extends from -0.5 to 0.5, then, on a coordinate system that is from -1 to 1, you'll get a quad that is half the size of the screen.

If you want another coordinate system, then you have to set up another coordinate system as well. Look up glOrtho to begin with.

K, thank you. I'm new to all this. Still learning.

#4 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 04 October 2012 - 08:48 AM

How do I go about changing it from -1 to 1?

#5 Brother Bob   Moderators   -  Reputation: 7923

Like
0Likes
Like

Posted 04 October 2012 - 09:22 AM

That's what you do with glOrtho.

It takes, ignoring the two last ones for the moment, two pairs of parameters. First one is the horizontal range of the visible coordinate system (from left to right), and the second one is the vertical range of the visible coordinate system (from bottom to top).

If you want the visible coordinate system to range from left=0 to right=640 along the horizontal direction, and from bottom=0 to top=480 along the vertical direction, you call:
glOrtho(0, 640, 0, 480, -1, 1);





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS