Drawing multiple shapes in OpenGL - c++

#include <Windows.h>
#include <GL\glut.h>
#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
bool init(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glMatrixMode(GL_PROJECTION);
// Set grid to be from 0 to 1
gluOrtho2D(0.0, 3.0, 0.0, 3.0);
return true;
}
void drawline(float from_x, float from_y, float to_x, float to_y)
{
// From coordinate position
glVertex2f(from_x, from_y);
// To coordinate position
glVertex2f(to_x, to_y);
}
void render(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 0.0); // Color (RGB): Yellow
glLineWidth(2.0); // Set line width to 2.0
glLoadIdentity();
// Draw line
glBegin(GL_LINES);
drawline(0.25, 0.5, 0.4, 0.5);
drawline(0.4, 0.6, 0.4, 0.5);
drawline(0.4, 0.4, 0.4, 0.5);
drawline(0.6, 0.5, 0.75, 0.5);
glEnd();
// Draw triangle
glBegin(GL_TRIANGLES);
glVertex2f(0.4, 0.5);
glVertex2f(0.6, 0.6);
glVertex2f(0.6, 0.4);
glEnd();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutCreateWindow("My OpenGL program");
init();
// Draw shape one
glPushMatrix();
glTranslatef(1.5, 1.5, 0.0);
glutDisplayFunc(render);
glPopMatrix();
// Draw shape two
glPushMatrix();
glTranslatef(2.5, 2.5, 0.0);
glutDisplayFunc(render);
glPopMatrix();
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
I'm working to draw something like this: http://i.imgur.com/JJpbJ7M.png
I don't need the whole thing, I just want to be able to draw two shapes where I want them to be. However, this isn't working, can anyone point me into the right direction?

You almost had it.
Draw your shape(s) in render(), not main():
#include <GL/glut.h>
void drawline(float from_x, float from_y, float to_x, float to_y)
{
// From coordinate position
glVertex2f(from_x, from_y);
// To coordinate position
glVertex2f(to_x, to_y);
}
void drawShape()
{
glColor3f(1.0, 1.0, 0.0); // Color (RGB): Yellow
glLineWidth(2.0); // Set line width to 2.0
// Draw line
glBegin(GL_LINES);
drawline(0.25, 0.5, 0.4, 0.5);
drawline(0.4, 0.6, 0.4, 0.5);
drawline(0.4, 0.4, 0.4, 0.5);
drawline(0.6, 0.5, 0.75, 0.5);
glEnd();
// Draw triangle
glBegin(GL_TRIANGLES);
glVertex2f(0.4, 0.5);
glVertex2f(0.6, 0.6);
glVertex2f(0.6, 0.4);
glEnd();
}
void render(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( 0.0, 4.0, 0.0, 4.0, -1, 1 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Draw shape one
glPushMatrix();
glTranslatef(1.5, 1.5, 0.0);
drawShape();
glPopMatrix();
// Draw shape two
glPushMatrix();
glTranslatef(2.5, 2.5, 0.0);
drawShape();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutCreateWindow("My OpenGL program");
glutDisplayFunc(render);
glutMainLoop();
return 0;
}
I removed reshape() and init() since the default glutReshapeFunc() already calls glViewport() and you should be resetting your projection and modelview matrices each frame anyway.

Related

OpenGL rotating glRectf() function?

I'am working on a project which one my homework. I need rotate a car(not exactly but like a car this is not important I think). My car 2d. I create it with glReactf(); function. Because of I can create a rectangle with this function pixel by pixel. Like;
// Create a rectangle (0,0) to (30,30) :) Like a square, yeap :) I use it.
// Because I am working on a lot of rectangles and you know, squares are rectangles in math :)
glRectf(0, 0, 30, 30);
But I have a code. It works but it is 3d. I can't turn it 2d. Can you help me? This is not important, I am working on 2d. While I say "I can't turn it 2d" I mean I didn't get algorithm and logic on this code. This is 3d quads code, and I can turn it with rotatef() function;
#include <stdio.h>
#include <GL/glut.h>
double rotate_y = 0;
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(rotate_y, 0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(0.5, -0.5, 0.5);
glColor3f(1.0, 1.0, 0.0);
glVertex3f(0.5, 0.5, 0.5);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(-0.5, 0.5, 0.5);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(-0.5, -0.5, 0.5);
glEnd();
glFlush();
glutSwapBuffers();
}
void keyboard(int key, int x, int y) {
if (key == GLUT_KEY_RIGHT) {rotate_y += 45;}
else if (key == GLUT_KEY_LEFT) {rotate_y -= 45;}
glutPostRedisplay();
}
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(0, 0);
glutInitWindowSize(800, 800);
glutCreateWindow("Rotating Test");
glutDisplayFunc(display);
glutSpecialFunc(keyboard);
glutMainLoop();
return 0;
}
I need a car(like 30x30 px quad in 2d) and I need turn it 180 degree on y axis. I want to create it like write it my first code.
I solve it, how I did it I don't know, I am serious but it done. This is my display function;
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(rotate_y, 0.0, 1.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
glRectf(1, 1, 11, 11);
glFlush();
glutSwapBuffers();
}

Performance drop with GL_LINES?

I tried to draw a simple line in OpenGL:
#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <freeglut.h>
float startX = -180.0f;
float startZ = -100.0f;
float qtyX = 36;
float qtyZ = 20;
float red = 0.0f, green = 0.0f, blue = 0.0f;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 50.0f, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(50.0f, 50.0f, 50.0f);
glEnd();
//glutWireSphere(20.0f, 20.0f, 20.0f);
glutSwapBuffers();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-64.0, 64.0, -36.0, 36.0, 10, 10000.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(1280, 720);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
However, when I run the program, the performance drop significantly then after a few seconds, it stops responding (I have to run task manager and force quit the program). However, when I disable the line and draw the glutWireSphere (or a polygon), the performance is normal.
Is there a problem with my code? Or perhaps GL_LINES is deprecated?
Windows 10, 1070 MaxQ, Visual Studio

OpenGL - The object moving everytime the menu clicked

I am developing a project and after I added menu for changing color,the object or shape in window changes color but keep moving other way or out of range every time an item in menu selected such as color. I also tried changing GLUT_DOUBLE to GLUT_SINGLE but still no luck.
#include "stdafx.h"
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#define RED 1
#define GREEN 2
#define BLUE 3
#define WHITE 4 //white colour for sphere
float red =1.0,green =1.0,blue=0.0,white = 0.0;
void init(void) {
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void){
// Sphere
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(red,green,blue);
glPushMatrix();
glTranslatef (-3.5, -1.5, 0.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (3.0, 3.0, 0.0);
glutSolidSphere(0.4,40,40);
glPopMatrix();
//Cone
glColor3f(0.0,0.0,0.0);
glTranslatef (0.95,-0.2, 0.0);
glPushMatrix();
glRotated(300,1.0,4.0,1.0);
glutSolidCone(0.6, 0.9, 30, 30);
glPopMatrix();
//Sphere(eye)
glColor3f(0.0,0.0,0.0);
glTranslatef (-0.75,0.8, 0.0);
glPushMatrix();
glScalef(0.6,0.6,0.0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
glFlush();
}
void reshape (int w, int h){
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -5.0);
}
void mouse(int option){
switch(option){
case RED:
red = 1.0,green = 0.0,blue = 0.0;
break;
case GREEN:
red = 0.0,green = 1.0,blue = 0.0;
break;
case BLUE:
red = 0.0,green = 0.0,blue = 1.0;
break;
case WHITE:
red = 1.0,green = 1.0,blue = 1.0;
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (700, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Testing");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
int sphere = glutCreateMenu(mouse); //change colour of pacman
glutAddMenuEntry("Red",RED);
glutAddMenuEntry("Green",GREEN);
glutAddMenuEntry("Blue",BLUE);
glutAddMenuEntry("White",WHITE);
glutCreateMenu(mouse);
glutAddSubMenu("Pacman",sphere);
glutAttachMenu(GLUT_RIGHT_BUTTON);
init ();
glutMainLoop();
return 0;
}
You have to do one glPopMatrix for each glPushMatrix. Ther is missing 1 glPopMatrix in the function display:
void display(void){
// Sphere
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(red,green,blue);
glPushMatrix();
glTranslatef (-3.5, -1.5, 0.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (3.0, 3.0, 0.0);
glutSolidSphere(0.4,40,40);
glPopMatrix();
//Cone
glColor3f(0.0,0.0,0.0);
glTranslatef (0.95,-0.2, 0.0);
glPushMatrix();
glRotated(300,1.0,4.0,1.0);
glutSolidCone(0.6, 0.9, 30, 30);
glPopMatrix();
//Sphere(eye)
glColor3f(0.0,0.0,0.0);
glTranslatef (-0.75,0.8, 0.0);
glPushMatrix();
glScalef(0.6,0.6,0.0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
glPopMatrix(); // <--------------------- this is missing
glFlush();
}
Since the pop is missing, the translation of the first rendering is kept on the top of the matrix stack. When the rendering is done a 2nd time with the new color, translation is applied again. This causes a progressive change of position.

OpenGL Can't draw z axis

Im very new to OpenGL and can't understand why my code doesn't work.
It is supposed to draw 3 axis: x y and z. But only x and y are displayed.
int main()
int main(int argc, char * argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(800, 600);
glutCreateWindow("OpenGL lesson");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void reshape ()
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-15, 15, -15, 15, -15, 15);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(1, 1, 1, 0);
}
void display()
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
// draw line for x axis
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(10.0, 0.0, 0.0);
// draw line for y axis
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 10.0, 0.0);
// draw line for Z axis
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 10.0);
glEnd();
glutSwapBuffers();
}
Could someone help, please?
Axiz Z is drawn, but due to the camera's default position, there will be just a single pixel drawn.
The camera's default position is looking in the Z direction, perpendicular to the XY plane. You might want to look into gluLookAt to place your camera elsewhere or gluPerspective for a different viewing volume (more commonly used for 3D rendering than glOrtho).

OpenGL projection question: Why I can't see anything on the screen?

void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
//glMatrixMode(GL_MODELVIEW);
//gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);
glMatrixMode(GL_PROJECTION);
gluPerspective(45, 2, -1, 1);
//glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar);
//gluPerspective(45.0, 45, -1, 1);
}
void displayFcn (void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_LINE);
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.1, 0.0, 0.0);
glVertex3f(0.50, 0.866025, 0.0);
glEnd();
glFlush();
}
void reshapeFcn(GLint newWidth, GLint newHeight)
{
glViewport(0,0,newWidth, newHeight);
winWidth = newWidth;
winHeight = newHeight;
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(400,200);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Test");
init();
glutDisplayFunc(displayFcn);
glutReshapeFunc(reshapeFcn);
glutMainLoop();
}
Could someone explain a little bit and give suggestions how to make that triangle visible.
I don't think that you're allowed to set 'zNear' negative. That will do weird things to the depth buffer and mean that you see things behind the eye. Docs say it's always positive. You're also liable to get somewhat strange results if you fix the aspect ratio to 2, regardless of the aspect ratio of the window.
You also need to set the current matrix back to MODEL_VIEW as datenwolf has shown. You don't need to use gluLookAt, but you need to do something to move the triangle away from the origin where the eye is located. You could do that by setting the z component to some negative value, or by applying a translation before the vertices:
glPushMatrix();
glTranslated(0,0,-5);
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.1, 0.0, 0.0);
glVertex3f(0.50, 0.866025, 0.0);
glEnd();
glPopMatrix();
gluPrespective and gluLookAt multiply on top of the current matrix on the selected stack. You need to load an identity first to make sense. Also you need to set a viewport before rendering. Best practice is to set all matrices and the viewport in the display function, and nowhere else. Sticking to that rule will make your life a lot easier. Also in OpenGL one usually doesn't have a dedicated initializtion phase. Resources are loaded on demand.
void displayFcn (void)
{
glViewport(0,0,winWidth, winHeight);
glClearColor (1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 2, 1, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);
glColor3f(0.0, 1.0, 0.0);
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_LINE);
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.1, 0.0, 0.0);
glVertex3f(0.50, 0.866025, 0.0);
glEnd();
glFinish();
}
void reshapeFcn(GLint newWidth, GLint newHeight)
{
winWidth = newWidth;
winHeight = newHeight;
glutPostRedisplay();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(400,200);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Test");
glutDisplayFunc(displayFcn);
glutReshapeFunc(reshapeFcn);
glutMainLoop();
}