473,411 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

OpenGL Problem with code

I have marked the areas with comments as to what I would like to do and
what the code does. The two problem areas are in the key pressing and
mousing clicking areas. thanks for the help.

This code outputs a set of lines on the screen in different colors
/*********************************************
** problem2 .c **
** --------------- **
** **
** A template OpenGL program using GLUT **
** **
** **
*********************************************/
#include "problem2.h"
/************************************************** **
** Globals! (In other words, I'm a C programmer) **
************************************************** **/
int screen_width = 512, screen_height = 512;
GLenum smooth = GL_FALSE;
GLenum statecoord = GL_TRUE;
GLenum done = GL_FALSE;
int markx1, marky1, markx2, marky2;
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
}
/************************************************** **
** display function -- what to draw **
************************************************** **/
void display ( void )
{
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0,0.0,0.0);
glBegin (GL_LINE_STRIP);
glVertex2i ( 1, 1 ); glVertex2i ( 1, 510 );
glVertex2i ( 1, 510 ); glVertex2i ( 510, 510 );
glVertex2i ( 510, 510 ); glVertex2i ( 510, 1 );
glVertex2i ( 510, 1 ); glVertex2i ( 1, 1 );
glEnd();
glColor3f(0.0,1.0,0.0);
glBegin(GL_LINES);
glVertex2i ( 32, 500 ); glVertex2i ( 82, 475 );
glVertex2i ( 30, 396 ); glVertex2i ( 90, 415 );
glVertex2i ( 82, 475 ); glVertex2i ( 30, 396 );
glEnd();
glColor3f(1.0,0.0,1.0);
glBegin(GL_LINES);
glVertex2i ( 100, 420 ); glVertex2i ( 147, 427 );
glVertex2i ( 104, 366 ); glVertex2i ( 149, 368 );
glVertex2i ( 102, 324 ); glVertex2i ( 150, 300 );
glVertex2i ( 102, 324 ); glVertex2i ( 104, 366 );
glVertex2i ( 147, 427 ); glVertex2i ( 149, 368 );
glEnd();
/* Blue colored lines */
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINES);
glVertex2i ( 176, 340 ); glVertex2i ( 223, 341 );
glVertex2i ( 176, 340 ); glVertex2i ( 185, 228 );
glVertex2i ( 237, 240 ); glVertex2i ( 185, 228 );
glEnd();
/* white colored lines */
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINES);
glVertex2i ( 250, 300 ); glVertex2i ( 250, 280 );
glVertex2i ( 248, 240 ); glVertex2i ( 253, 220 );
glEnd();
/* Yellow colored lines */
glColor3f(1.0,1.0,0.0);
glBegin(GL_LINES);
glVertex2i ( 293, 249 ); glVertex2i ( 307, 286 );
glVertex2i ( 307, 286 ); glVertex2i ( 307, 144 );
glVertex2i ( 264, 145 ); glVertex2i ( 347, 148 );
glEnd();
/* Cyan colored lines */
glColor3f(0.0,1.0,1.0);
glBegin(GL_LINES);
glVertex2i ( 415, 251 ); glVertex2i ( 371, 233 );
glVertex2i ( 371, 233 ); glVertex2i ( 376, 201 );
glVertex2i ( 376, 201 ); glVertex2i ( 431, 151 );
glVertex2i ( 431, 151 ); glVertex2i ( 368, 102 );
glEnd();
/* Orange colored lines */
glColor3f(1,.5,0);
glBegin(GL_LINES);
glVertex2i ( 444, 174 ); glVertex2i ( 474, 179 );
glVertex2i ( 474, 179 ); glVertex2i ( 442, 38 );
glVertex2i ( 391, 38 ); glVertex2i ( 493, 45 );
glEnd();

/* triggered if both endpoints in x and y **
** area to draw the lines with the mouse clicks */
if(done)
{
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINES);
glVertex2i ( markx1, marky1 );
glVertex2i ( markx2, marky2 );
glEnd();
}
/* flush GL commands & swap buffers */
glFlush();
glutSwapBuffers();

}
/************************************************** **
** idle function... what to do when bored **
************************************************** **/
void idle ( void )
{
}
/************************************************** **
** deal with user resizing window (BAD USER!) **
************************************************** **/
void reshape( int w, int h )
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-512.0, 512.0, -512.0, 512.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/************************************************** **
** deal with mouse movement **
************************************************** **/
void motion(int x, int y)
{
}
/************************************************** **
** deal with mouse clicks **
** in this area I want the right mouse button to **
** click on two areas and draw a line but it does **
** not draw the line where the mouse clicks it **
** draws it somewhere completely else **
************************************************** **/
void button(int b, int st, int x, int y)
{
switch ( b )
{
default:
break;

case GLUT_LEFT_BUTTON:
{
switch( st )
{
case GLUT_DOWN:
break;
case GLUT_UP:
break;
}
}
break;
case GLUT_MIDDLE_BUTTON:
{
switch( st )
{
case GLUT_DOWN:
break;
case GLUT_UP:
break;
}
}
break;
case GLUT_RIGHT_BUTTON:
{
switch( st )
{
case GLUT_DOWN:
if(statecoord)
{
markx1 = x; marky1 = y;
}
else
{
markx2 = x; marky2 = y;
glutPostRedisplay();
done = GL_TRUE;
}
break;
case GLUT_UP:
statecoord = GL_FALSE;
break;
}
}
break;
}
}
/************************************************** **
** deal with menu commands **
************************************************** **/
void menu( int value )
{
switch (value)
{
default:
break;

case 27: /* quit */
exit(0);
break;
}
glutPostRedisplay();

}
/************************************************** **
** deal with key strokes **
** in this area I would like the c button to change**
** the ShadeModel from GL_SMOOTH to GL_FLAT and **
** vice versa. this will create the colors to **
** change along the line **
************************************************** **/
void keys(unsigned char key, int x, int y)
{
switch ( key )
{
default:
break;
case 'Q':
exit(0);
break;
case 'q':
exit(0);
break;
case 'C':
smooth = !smooth;
if (smooth) {
glShadeModel(GL_SMOOTH);
} else {
glShadeModel(GL_FLAT);
}
glutPostRedisplay();
break;
case 'c':
smooth = !smooth;
if (smooth) {
glShadeModel(GL_SMOOTH);
} else {
glShadeModel(GL_FLAT);
}
glutPostRedisplay();
break;
}
}
/************************************************** **
** extract the parameters from the command line **
************************************************** **/
void ExtractCommandLineParameters( int argc, char **argv )
{
int i;

printf("\n");
/* while there are parameters to deal with */
for ( i = 1; i < argc; i++ )
{
if (!strcmp(argv[i], "-help"))
{
printf("Usage: %s [options] \n\n", argv[0]);
printf("Options:\n");
printf("\t-help Print this message and exit\n");
printf("\t-debug Print dubugging messages during
execution\n");
exit(0);
}
if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "-debug"))
{
/* put debug switches here */
i++;
}
}

}
/************************************************** **
** main entry point **
************************************************** **/

int main(int argc, char** argv)
{
/* Do all the general purpose startup stuff...*/
glutInit(&argc, argv);
/* get command line parameters */
ExtractCommandLineParameters( argc, argv );
/* we got a RGBA buffer and we're double buffering! */
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
/* set some size for the window. */
glutInitWindowSize( screen_width, screen_height );
/* arbitrarily set an initial window position... */
glutInitWindowPosition( 100, 100 );
/* make the window. give it a cool title */
glutCreateWindow("Courtney Hayward Problem2");
/* set the callback functions */
init();
glutReshapeFunc( reshape );
glutIdleFunc( idle );
glutMouseFunc( button );
glutMotionFunc( motion );
glutKeyboardFunc( keys );
glutDisplayFunc( display );
/* setup the menu */
/* probably will do something here eventually... */
/* start it! */
glutMainLoop();
return 0

Nov 15 '05 #1
2 1709
George wrote:
I have marked the areas with comments as to what I would like to do and
what the code does. The two problem areas are in the key pressing and
mousing clicking areas.


I didn't find a concise, short description of what it does and what you
think it should do, sorry. Also, make a _minimal_ example.

I only noticed two things:
1. your code in button() is ill-indented
2. you can bundle case labels, i.e. if 'c' and 'C' should do the same, you
can write

switch(...) {
case 'c':
case 'C':
// common code here
break;
}
Uli
Nov 15 '05 #2
George wrote on 04/09/05 :
** Globals! (In other words, I'm a C programmer) **


ITYM

** Globals! (In other words, I'm not a C programmer) **

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Rick Muller | last post by:
I have a problem that I would like to get some advice on from other Pythonistas. I currently manage a (soon to be) open source project for displaying molecular graphics for a variety of different...
15
by: oracle411 | last post by:
Hi I'm new to OpenGL, but have an OpenGL application written in CPP that I would like to display on a web browser. Was wondering how this could be done? Thank you very much
10
by: Bad_Kid | last post by:
which is better for what?
0
by: Scott Chang | last post by:
Hi all, I tried to use Managed C++ coding of VC++.NET (2002)and OpenGL version 1.2 to draw a hexagon shape of benzene. My OpenGLdrawBenzene.cpp is the following: // This is the main project file...
1
by: Scott Chang | last post by:
Hi All I tried to use the attached OpenGL code to draw a Star-shaped figure on my Microsoft Visual C++ .NET 2002-Windows XP Pro PC. When I did 'Build' in Debug Mode, I got an error C2065:...
8
by: NaeRey | last post by:
Hey, I've been searching for something like a PyOpenGL implementation that allows Python to use OpenGL, found only a few projects and most are either in beta and dead, or alpha stage. Anyone knows...
14
by: Jessica Weiner | last post by:
I am writing an application in C# which need to plot graphs and simple shapes (polygons, circles, squares etc). Which library is better for this purpose and why? Thanks.
2
by: leszpol | last post by:
On http://www.uni-koblenz.de/~yasko/g95gl/ the autor describes how to attach f90gl OpenGL library to g95 compiler under Windows nad Unix. He gives all indications to attach library and says tahat...
3
by: jg.campbell.ng | last post by:
I'm beginning learning Python and OpenGL in Python. Python fine. But difficulties with OpenGL; presumably with the installation of OpenGL. OS = Linux FC5. Python program gl_test.py: from...
1
by: linksterman | last post by:
hey, I was experimenting with pyglet, and was trying out their opengl, but cant figure out how to scale images... #!/usr/bin/python # $Id:$ '''Demonstrates one way of fixing the display...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.