Thread: OpenGL example: whats not working here?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    244

    OpenGL example: whats not working here?

    hi!

    can anybody tell me, whats wrong with that code? the polygon i'm attempting to draw doesnt appear on the screen, but as in the original example, the 2d quad does appear on the screen!?!

    Sample.cpp:
    Code:
    #include <SDL/SDL.h>
    #include "glut.h"
    #include "Engine.h"
    
    void RefreshScreen() {
    	glClearColor(0, 0, 0, 0);
    	glClear(GL_COLOR_BUFFER_BIT);
    
    	glLoadIdentity();
    	//working properly:
    	/*glBegin(GL_QUADS);
    		glColor3ub(255, 0, 0);
    		glVertex2i(0, 240);
    		glColor3ub(0, 255, 0);
    		glVertex2i(240, 240);
    		glColor3ub(0, 0, 255);
    		glVertex2i(240, 480);
    		glColor3ub(255, 255, 0);
    		glVertex2i(0, 480);
    	glEnd();*/
    
    	//not working:
    	glBegin(GL_TRIANGLES);
    		glColor3ub(255, 0, 0);
    		glVertex3f(-1, -.5, 3);
    		glColor3ub(0, 255, 0);
    		glVertex3f(1, -.5, 3);
    		glColor3ub(0, 0, 255);
    		glVertex3f(0, .5, 3);
    	glEnd();
    }
    
    int main(int argc, char *argv[]) {
    	EngineInit(640, 480, 32);
    
    	bool done = false;
    	while(!done) {
    		SDL_Event event;
    		while(SDL_PollEvent(&event)) {
    			switch(event.type) {
    				case SDL_KEYDOWN:
    				case SDL_QUIT:
    					done = true;
    					break;
    			}
    		}
    		RefreshScreen();
    		SDL_GL_SwapBuffers();
    	}
    	EngineFree();
    
    	return(0);
    }
    Engine.cpp:
    Code:
    #include <SDL/SDL.h>
    #include "glut.h"
    
    class Engine {
    public:
    	SDL_Surface *screen;
    };
    Engine engine;
    
    void EngineInit(int width, int height, int depth) {
    	SDL_Init(SDL_INIT_VIDEO);
    	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, 16);
    	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    	engine.screen = SDL_SetVideoMode(width, height, depth, SDL_OPENGL);
    
    	glViewport(0, 0, engine.screen->w, engine.screen->h);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	glOrtho(0, engine.screen->w, engine.screen->h, 0, -1, 1);
    	glMatrixMode(GL_MODELVIEW);
    }
    
    void EngineFree() {
    }
    thanks for upcoming help
    Last edited by Devils Child; 02-03-2008 at 06:32 AM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The two snippets are char-by-char identical. What makes you think one works and the other doesn't?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    oh i'm sorry, i posted the wrong example. give me a minute and i'll post the correct lines!

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    here ya go:
    Code:
    	//working properly:
    	/*glBegin(GL_QUADS);
    		glColor3ub(255, 0, 0);
    		glVertex2i(0, 240);
    		glColor3ub(0, 255, 0);
    		glVertex2i(240, 240);
    		glColor3ub(0, 0, 255);
    		glVertex2i(240, 480);
    		glColor3ub(255, 255, 0);
    		glVertex2i(0, 480);
    	glEnd();*/
    
    	//not working:
    	glBegin(GL_TRIANGLES);
    		glColor3ub(255, 0, 0);
    		glVertex3f(-1, -.5, 3);
    		glColor3ub(0, 255, 0);
    		glVertex3f(1, -.5, 3);
    		glColor3ub(0, 0, 255);
    		glVertex3f(0, .5, 3);
    	glEnd();
    thats what is not working.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    so? why do i get a black screen?
    did i forget anything during the openGL setup?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The triangle is very tiny compared to the square. Are you sure it's not simply too small to see?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    hm, when i shift it two decimal points right it doesnt work either...
    Code:
    	glBegin(GL_TRIANGLES);
    		glColor3ub(255, 0, 0);
    		glVertex3f(-100, -50, 3);
    		glColor3ub(0, 255, 0);
    		glVertex3f(100, -50, 3);
    		glColor3ub(0, 0, 255);
    		glVertex3f(0, 50, 3);
    	glEnd();
    this is 3d projection. so that it doesnt work shows me that something with the projection must be wrong...
    vertex2i, which is 2d and not 3d works. and the 3d thingy which has to be projected fails

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Code:
    glOrtho(0, engine.screen->w, engine.screen->h, 0, -1, 1);
    man glOrtho:

    Code:
    Name
      glOrtho — multiply the current matrix with an orthographic matrix
    C Specification
      void glOrtho(  GLdouble    left, 
         GLdouble    right, 
         GLdouble    bottom, 
         GLdouble    top, 
         GLdouble    nearVal, 
         GLdouble    farVal);
    Parameters
      left, right
       Specify the coordinates for the left and right vertical clipping planes. 
      bottom, top
       Specify the coordinates for the bottom and top horizontal clipping planes. 
      nearVal, farVal
       Specify the distances to the nearer and farther depth clipping planes. These values are negative if the plane is to be behind the viewer.
    So you tell OpenGL that you want to render all those (parts of) objects that are within the volume specified by [left, bottom, nearVal] - [right, top, farVal]. I.e. show only those objects that have x coordinates between left and right, y coordinates between bottom and top, and z coordinates between nearVal and farVal.

    Look what you are doing:

    Code:
    glBegin(GL_TRIANGLES);
        glColor3ub(255, 0, 0);
        glVertex3f(-1, -.5, 3);
        glColor3ub(0, 255, 0);
        glVertex3f(1, -.5, 3);
        glColor3ub(0, 0, 255);
        glVertex3f(0, .5, 3);
    glEnd();
    The triangle has a base from x coordinate -1 to +1 -> only half of the triangle can be shown as you specified that the viewing volume ranges from 0 on upwards.
    Its height varies from -5 at the base to +5. Again, y coordinates below 0 will not be shown as you told OpenGL not to show them.
    The z position of the triangle you just specified at 3. Again, this does not fall within the visible volume, since you specified a range of -1 to +1 in your call to glOrtho.

    Now I will leave it up to you to find out how to fix this.

    Some additional comments:
    * Before you have the basics of SDL + OpenGL down, I would suggest not even considering commands such as the following:

    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, 16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    * You seem to want to use a class based design for your engine. Instead of having a global engine variable hanging around somewhere in your code, you should seriously consider using a singleton and wrapping EngineInit into the constructor, and EngineFree into the destructor.
    Last edited by MWAAAHAAA; 02-03-2008 at 11:29 AM.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    hm. i noticed that znear was -1 as it was describet in the example, i took the code from. of course thats wrong, so znear->zfar is 0.1->100 now.
    and i still get a black screen

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by Devils Child View Post
    hm. i noticed that znear was -1 as it was describet in the example, i took the code from. of course thats wrong
    Why was that wrong?

    so znear->zfar is 0.1->100 now.
    Why do you think this would be right?

    and i still get a black screen
    That is to be expected

  11. #11
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Your triangle is sitting at a depth of 3. Call glTranslatef(0.0, 0.0, -4.0);.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithread pthread with OpenGL
    By parad0x13 in forum C++ Programming
    Replies: 8
    Last Post: 07-24-2008, 03:04 PM
  2. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  3. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  4. Replies: 0
    Last Post: 07-26-2007, 09:55 AM
  5. .TGA files in OpenGL
    By ... in forum Game Programming
    Replies: 2
    Last Post: 10-26-2003, 02:48 PM