• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

How do I get my DrawCone function to appear?

Experts,

        I am working on a program that I want a sphere at the origin and a triangle attached to form a cone. I tested the function in a small Opengl program and it works fine. I added to a project that I am building on and when I execute the screen returns a black screen only - no errors. My code is below:


// DrawCone.cpp : Defines the entry point for the console application.
//
#include "stdafx.h" //put this first or weird errors occur
#include <stdlib.h>
#include <glut.h>
#include <gl.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#define PI 3.14159


double user_theta  = 0;
double user_height = 0;
    
 void RenderScene(void) 
 {
   glClear(GL_COLOR_BUFFER_BIT);

   //Reset the modelview matrix
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   //Translate the whole scene out and into view
   //This is initial viewing transformation

   glTranslatef(0.0f, 0.0f, 0.0f);//at origin

   //Draw a sphere at the origin
   glColor3ub(255, 0, 0);
   glScalef(0.2, 0.2, 0.2); //scale sphere
   glutWireSphere(1.0f, 15, 15); //size of sphere
   glPushMatrix();//save viewing transformation

   //draw a triangle   
   glColor3ub(255, 0, 0);
   glTranslatef(0.7f, 0.0f, 0.0f);//at out from origion
   glRotatef(480.0f, 10.0f, 10.0f, 10.0f); 
   glScalef (1.0, 2.0, 1.0);      /* modeling transformation */ 
   glutWireCone (1.0, 2.0, 3, 4);
   glPopMatrix(); //Restore the viewing transformation
   //glTranslatef(10.0f, 0.0f, 0.0f);
   //glutSolidSphere(1.0f, 15, 15);
   glFlush();
}



GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0};  /* Red diffuse light. */
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};  /* Infinite light location. */

GLfloat XAngle = 0;
GLfloat YAngle = 0;
GLfloat ZAngle = 0;

GLfloat t = 0.0;
GLfloat Deltat = 0.1;

GLfloat twists = 4, bends = 6;

void
stroke_output(GLfloat Scale, GLfloat x, GLfloat y, GLfloat z, char *format,...)
{
  va_list args;
  char buffer[200], *p;

  va_start(args, format);
  vsprintf(buffer, format, args);
  va_end(args);
  glPushMatrix();
  glTranslatef(x, y, z);
  glScalef(0.005*Scale, 0.005*Scale, 0.005*Scale);
  for (p = buffer; *p; p++)
    glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
  glPopMatrix();
}


void
drawAxes(void) 
{

  glDisable(GL_LIGHTING);
   glBegin(GL_LINES);
   glColor3f(1.0, 1.0, 1.0);
   glVertex3f(-2.0, 0.0, 0.0);
   glVertex3f( 2.0, 0.0, 0.0);
   glVertex3f( 0.0, -2.0, 0.0);
   glVertex3f( 0.0,  2.0, 0.0);
   glVertex3f( 0.0, 0.0, -2.0);
   glVertex3f( 0.0, 0.0, 2.0);
   glEnd();
   
   stroke_output(0.5, 2.0, 0.0, 0.0, "X");
   stroke_output(0.5, 0.0, 2.0, 0.0, "Y");
   stroke_output(0.5, 0.0, 0.0, 2.0, "Z");
}




//Make sure the perspective does not become distorted when the window size is changed
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	// Reset the coordinate system before modifying
// Adjust cube position to be asthetic angle. */
	glMatrixMode(GL_MODELVIEW); //projection defines the viewing volume. 
	glLoadIdentity(); //load identity matrix to initialize it. 
	gluLookAt(0.0,0.0,5.0, //camera position eye is at (0,0,5) 
		     0.0, 0.0, 0.0,      // center is at (0,0,0) *//defines the point we're looking at
			  //0.0f,1.0f,0.0f; //up vector (usually (0.0,1.0, 0.0)- camera not tilted. (turn everything upside down (0.0, -1.0, 0.0) 
              0.0, 1.0, 0.); // up is in positi_ve Y direction */
   glTranslatef(0.0, 0.0, -1.0);
   glRotatef(XAngle, 1.0, 0.0, 0.0);
   glRotatef(YAngle, 0.0, 1.0, 0.0);
   drawAxes();
   glColor3f(0.0, 0.0, 1.0);
   glShadeModel(GL_SMOOTH);
   RenderScene();
   glutSwapBuffers();//Show the image

}

void
init(void)
{

  
  /* Use depth buffering for hidden surface elimination. */
  glEnable(GL_DEPTH_TEST);

  /* Setup the _view of the cube. */
  glMatrixMode(GL_PROJECTION);
  gluPerspective( /* field of view in degree */ 40.0,
    /* aspect ratio */ 1.0,
    /* Z near */ 1.0, /* Z far */ 10.0);
  glMatrixMode(GL_MODELVIEW);
  gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
    0.0, 0.0, 0.0,      /* center is at (0,0,0) */
    0.0, 1.0, 0.);      /* up is in positi_ve Y direction */

  /* Adjust cube position to be asthetic angle. */
  glTranslatef(0.0, 0.0, -1.0);
  glRotatef(XAngle, 1.0, 0.0, 0.0);
  
  glEnable(GL_NORMALIZE);

}



void moveit(int x, int y) {

/* Set X and Y rotation angle to mouse position */
/* Not elegant, but it works */
  XAngle = x;
  YAngle = y;
  glutPostRedisplay();

}

void HaltRotate(int status, int x, int y) {

if (status == GLUT_MENU_IN_USE) 
   glutMotionFunc(NULL);
else
   glutMotionFunc(moveit);

}

void HandleMenu(int choice) {


   switch (choice) {
   
     case 1: twists ++; break;
     
     case 2: if (twists != 1) twists --; break;
     
     case 3: bends ++; break;
     
     case 4: if (bends != 1) bends --; break;
     
     }
     
     glutPostRedisplay();
 
  
  }


void initMenus(void) {

  glutCreateMenu(HandleMenu);
  glutAddMenuEntry("More twists", 1);
  glutAddMenuEntry("Fewer twists", 2);
  glutAddMenuEntry("More bends", 3);
  glutAddMenuEntry("Fewer bends", 4);
  glutAttachMenu(GLUT_RIGHT_BUTTON);
  
  }


//main function that performs required initializations and start the event processing loop
int main(int argc, char **argv) { // argc - pointer to the unmodified argc variable, argv - pointer to the unmodified argv variable from main funciton
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); //mode specifies the display mode of color and buffers
	glutCreateWindow("DrawCone"); //Set window title
	initMenus();
	glutMenuStatusFunc(HaltRotate);
    glutDisplayFunc(display);
    glutMotionFunc(moveit);
    init();
	glutMainLoop();
}

Open in new window

0
missymadi
Asked:
missymadi
1 Solution
 
ikeworkCommented:
Hi missymadi,

The problem was:

1) The color buffer was cleared in "RenderScene", so everything that was rendered in display() before was cleared
2) the "glLoadIdentity();" call in "RenderScene" messed up the previous camera setting from the function display()

I attached the changed function RenderScene() below, it works here.

ike
void RenderScene(void) 
 {
   //Translate the whole scene out and into view
   //This is initial viewing transformation

   glTranslatef(0.0f, 0.0f, 0.0f);//at origin

   //Draw a sphere at the origin
   glColor3ub(255, 0, 0);
   glScalef(0.2, 0.2, 0.2); //scale sphere
   glutWireSphere(1.0f, 15, 15); //size of sphere
   glPushMatrix();//save viewing transformation

   //draw a triangle   
   glColor3ub(255, 0, 0);
   glTranslatef(0.7f, 0.0f, 0.0f);//at out from origion
   glRotatef(480.0f, 10.0f, 10.0f, 10.0f); 
   glScalef (1.0, 2.0, 1.0);      /* modeling transformation */ 
   glutWireCone (1.0, 2.0, 3, 4);
   glPopMatrix(); //Restore the viewing transformation
   //glTranslatef(10.0f, 0.0f, 0.0f);
   //glutSolidSphere(1.0f, 15, 15);
   glFlush();
}

Open in new window

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now