GIDForums  

Go Back   GIDForums > Computer Programming Forums > OpenGL Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Nov-2006, 13:19
Seraphis Seraphis is offline
Junior Member
 
Join Date: Jul 2006
Posts: 32
Seraphis is on a distinguished road

OpenGL DrawScene help


O.K. So I've been learning a lot of OpenGL and I was messing around with drawing shapes when I got the idea of making a triangle, which could via the W,A,S,D keys have its vertices moved about the screen to reshape it.

I got it working with 1 vertex, just by making a GLfloat , and manipulating it with
CPP / C++ / C Code:
 if (keys['S']) { blah blah } 

The theory I came up with to make this possible, was to have multidimensional arrays which used a variable in the brackets to change the vertex every time the user hits enter.... Heres what I got.

keep in mind this is just a small piece out of a simple OpenGL framework

**My Variables**
CPP / C++ / C Code:
GLfloat p1[10];
GLfloat p2[10];
GLfloat p3[10];
int stc=0;

CPP / C++ / C Code:
int DrawGLScene(GLvoid)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	
	glTranslatef(0,0,0);

	glRotatef(rtri,0.0f,1.0f,0.0f);
	glRotatef(rtriz,1.0f,0.0f,0.0f);

	glBegin(GL_TRIANGLES);
			glVertex3f(p1[0],p2[0],p3[0]);
			glVertex3f(p1[1],p2[1],p3[1]);
			glVertex3f(p1[2],p2[2],p3[2]);
	glEnd();

	if (keys['W'])
	{
		keys['W'] = FALSE;
		p1[stc]+=0.1f;
	}
	if (keys['S'])
	{
		keys['S'] = FALSE;
		p1[stc]-=0.1f;
	}
	if (keys[VK_RETURN])
	{
		if (stc < 3)
		{
			stc++;
		}
		else if (stc > 2)
		{
			stc = 0;
		}
	}

	return TRUE;										//
}

In theory, this should make it so you can cycle through the 3 vertices and moved the X position, positive or negative.

I get no errors when I compile it, but when I run it theres no triangle... which makes sense considering all of the points on it are 0.0f . But when I push 'W' to bring the 1st vertex up, nothing happens.

Thoughts?

Thx in advance,
Seraphis
  #2  
Old 21-Nov-2006, 16:44
Seraphis Seraphis is offline
Junior Member
 
Join Date: Jul 2006
Posts: 32
Seraphis is on a distinguished road

Re: OpenGL DrawScene help


Eh no takers? ;D
  #3  
Old 25-Nov-2006, 04:52
davis
 
Posts: n/a

Re: OpenGL DrawScene help


Quote:
Originally Posted by Seraphis
Eh no takers? ;D

In looking at your code, we see no initialization of the p1 global before being used in the 'W' key related code that you mention. What is the value of p1[stc] when you step through your code with the debugger?

How can we possibly know based on the posted code? Maybe that's why no one cares to answer the post? How do we even know that the 'W' key code block is being executed?

In your code, you're passing p1, p2, p3 into glVertex3f, but your code never modifies the p2 or p3 float arrays. In fact, your code seems only to attempt to modify p1[0..2], which one would think would be what you would want to pass into glVertex3f. You also declare each of these float arrays to be 10 elements deep. Why? ...if you plan only to use 3, why not declare it as such?

I think that you're encountering an oversight in what you mean to do with the arrays, however, more importantly, I think that you're missing some rather obvious fundamentals of programming. You may want to review the basics of C programming before attempting a rather complex API like OpenGL. Since OpenGL gives the programmer direct access to the video hardware, it is not terribly difficult to lock-up the system with a few errant lines of code.

Here is a thought. Write a piece of code that draws a known triangle based on variables that you've initialized in your array(s). Once successfully rendered, then modify the values in the array(s) using the "user interface" code. The idea of using known-good data first and then changing it is not something that I invented, but you may find it a practical choice in lieu of first gaining more experience in C programming.

Also, start using a debugger everytime your code doesn't do what you expect. Know that your data members are being initialized to the values that you expect. Always initialize your variables before you use them.


:davis:
  #4  
Old 26-Nov-2006, 17:59
Seraphis Seraphis is offline
Junior Member
 
Join Date: Jul 2006
Posts: 32
Seraphis is on a distinguished road

Re: OpenGL DrawScene help


Yeh, sorry for the short posting. I was just trying to not post a novel... I like the constructive criticism tho, it does help. I will try this again with a more in-depth look at it.

Thank you,
Seraphis
  #5  
Old 05-Dec-2006, 17:26
davis
 
Posts: n/a

Re: OpenGL DrawScene help


Quote:
Originally Posted by Seraphis
Yeh, sorry for the short posting. I was just trying to not post a novel... I like the constructive criticism tho, it does help. I will try this again with a more in-depth look at it.

Thank you,
Seraphis

Maybe this boiler-plate code will help some?

CPP / C++ / C Code:
#include <GL/glut.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define UNUSED_PARAMETER(x) ((void)(x))

static GLfloat g_edge = 0.0F;

void rotateBox()
{
    g_edge += 2.0F;
    if( g_edge > 360.0F )
    {
	g_edge -= 360.0F;
    }
    glutPostRedisplay();
}

void display()
{
    /* fprintf( stderr, "display\n" ); */
    glClear( GL_COLOR_BUFFER_BIT );
    glPushMatrix();
    glRotatef( g_edge, 0.0F, 0.0F, 1.0 );
    glColor3f( 1.0, 1.0, 1.0 );
    glRectf( -25.0F, -25.0F, 25.0F, 25.0F );
    glPopMatrix();
    glutSwapBuffers();
}

void
init()
{
    /* fprintf( stderr, "init\n" ); */
    glShadeModel( GL_FLAT );
}

void
reshaped( int w, int h )
{
    /* fprintf( stderr, "reshaped w = %d h = %d\n", w, h ); */
    glViewport( 0, 0, (GLsizei)w, (GLsizei)h );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( -50.0F, 50.0F, -50.0F, 50.0F, -1.0F, 1.0F );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
}

void
keypressed( unsigned char key, int x, int y )
{
    /* fprintf( stderr, "keypressed key = %c x = %d y = %d\n", key, x, y ); */
    UNUSED_PARAMETER( x );
    UNUSED_PARAMETER( y );
    switch( key )
    {
	case 's':
	case 'S':
	    glutIdleFunc( NULL );
	    break;
	case 'r':
	case 'R':
	    glutIdleFunc( rotateBox );
	    break;
	default:
	    break;
    }
}

void
mousepressed( int button, int state, int x, int y )
{
    fprintf( stderr, "mousepressed button = %d state = %d x = %d y = %d\n", button, state, x, y );
}

void
mousemoved( int x, int y )
{
    fprintf( stderr, "mousemoved x = %d y = %d\n", x, y );
}

void
idle()
{
    /* do not use unless you must handle idle cycles */
}

int main( int argc, char* argv[] )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
    glutInitWindowSize( 640, 480 );
    glutInitWindowPosition( 100, 100 );
    glutCreateWindow( "OpenGL Window" );
    init();
    glutDisplayFunc( display );
    glutReshapeFunc( reshaped );
    glutKeyboardFunc( keypressed );
    glutMouseFunc( mousepressed );
    glutMotionFunc( mousemoved );
    /*glutIdleFunc( idle ); */

    glutMainLoop();

    return 0;
}


...use "r" to rotate and "s" to stop.

:davis:
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OpenGL to clipboard rodrigomorante OpenGL Programming 0 21-Dec-2005 19:41
What is OpenGL? Shrikharan OpenGL Programming 6 21-Dec-2005 17:47
C++ game programming OpenGL oozsakarya OpenGL Programming 2 13-Nov-2005 13:50
nVidia & OpenGL Problems in windows XP marjasin Computer Hardware Forum 21 30-Aug-2004 00:57

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:30.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.