simple OpenGL, but won't draw anything.

simple OpenGL, but won't draw anything. Suggestions??

How lucky you are to be reading my very first post on cplusplus.
Be gentle...

Trying to finish my degree (on the 35 year program), taking Graphics using OpenGL.
Trying to create this seemingly simply program and make it draw.
It is a pseudocode example from the text book.
It compiles and runs without errors, but all I get is a blank white screen.
It should be drawing GL_LINE_STRIPs.

Suggestions??
Thanks.

// Experiment 2
// Experimentation with scaling and translation
// 6 Oct 2015

#include <windows.h>
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <time.h>
#include <gl/Gl.h>
#include <gl/Glu.h>
#include <gl/Glut.h>
using namespace std;

#define PI 3.14159265

// ******************************************************************
// Global Variables

const float screenWidth = 800.0; // max screen width in pixels
const float screenHeight = 600.0; // max screen height in pixels
float color[] = { 0.0, 0.0, 0.0 }; // color
GLdouble A = screenWidth / 4.0; // scaling and translation variables
GLdouble B = 0.0;
GLdouble C = screenHeight / 2.0;
GLdouble D = C;

// ******************************************************************
// Function Declarations

void myInit(void); // my initialization routine
void myDisplay(void); // the redraw function
void Random_Color(); // generate a random color
float f(float x); // any f(x) I want
void Print_Msg(int, char*); // Print messages to screen

// ******************************************************************

int main(int argc, char* argv[])
{
srand(time(NULL)); // seed RNG
glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set the display mode
glutInitWindowSize(screenWidth, screenHeight); // set the window size

glutInitWindowPosition(0, 0); // set window starting position

glutCreateWindow("Experiment 2"); // open the screen window
glutDisplayFunc(myDisplay); // register the redraw function
myInit(); // call my initialization routine
glFlush(); // send everything to screen
glutMainLoop(); // go into a perpetual loop

}

// ******************************************************************
// START FUNCTION DEFINITIONS
// ******************************************************************

// ******************************************************************
// myInit()

void myInit(void) {

glClearColor(1.0, 1.0, 1.0, 0.0); // the background color is white
// glColor3f(0.0f, 0.0f, 0.0f); // the drawing color is black
glPointSize(2.0); // a 'dot' is 2 by 2 pixels
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, screenWidth, 0.0, screenHeight);
}

// ******************************************************************
// myDisplay()

void myDisplay(void) {

glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glColor3f(color[0], color[1], color[2]); // set drawing color

glBegin(GL_LINE_STRIP); // Draw line strips
for (int x = 0; x <= 300; x += 3) { // scaled and
glVertex2d(A * x + B, C * f(x) + D); // translated
}
glEnd();
glFlush(); // send all output to display

}

// ******************************************************************
// Random_Color()

void Random_Color(void) {
// This routine will generate a random color and store it in color[0-2].
// color[] is a global array of floats.

for (int c = 0; c < 3; c++) { color[c] = ((float)(rand() % 100) / 100); }
}


// ******************************************************************
// f()

float f(float x) {
// any f(x) I want

return cos(2 * PI * x);
}


// ******************************************************************
// Add next function here


// ******************************************************************
// Print_Msg()

void Print_Msg(int Msg, char* Message) {
// Receives an Int value and a char string
// If the INT is zero, simply print the string argument.
// If the INT is non-zero, print the corresponding message from the
// list below, ignoring the string, if any, supplied as the 2nd arg.

switch (Msg) {
case 0: cout << endl << Message; // print msg as given
break;
case 1: cout << endl << "Error 1" << endl; // Placeholder
break;
case 2: cout << endl << "Error 2" << endl; // Placeholder
break;
default: break;
} // end SWITCH
cout << endl; // pad a line before returning
} // end function. Returns nothing.
Last edited on
Someone at opengl.org helped figure this out.

I am sending x as an integer to the function f(x) which is expecting a float.
The function f(x) uses that int int the formula to compute cosine causing cosine to always return a 1.
On returning, f(x) = 1 always and so the line strip is always being drawn right on the edge of the window, which I cannot see.
Modifying the code to call f((float)x), casting x to float, is part of the solution.
But for some reason, casting the entire cosine argument (which it expects as a double) to an int as in cos((int)(2.0 * PI * x)), seems to be the other part of the solution.
At least the code is drawing something to the screen and so I can simply play around with getting the argument types correct.
Thanks for looking.
Topic archived. No new replies allowed.