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).
Related
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();
}
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.
#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.
#include <GL/glut.h>
GLint winWidth = 600, winHeight = 600;
GLfloat x0 = 100.0, y0 = 100.0, z0 = 50.0;
GLfloat xref = 50, yref = 50.0, zref = 0.0;
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;
GLfloat xwMin = -40.0, ywMin = -60.0, xwMax = 40.0, ywMax = 60.0;
GLfloat dnear = 25.0, dfar = 125.0;
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);
glLoadIdentity();
//glOrtho(0,1,0,1, 0,0.1);
//gluOrtho2D(0, 1,0,1);
//gluPerspective(45, 1.2, 1, 10);
glFrustum(0, 1, 0, 1, 0, 1);
//gluPerspective(45.0, 1, 1, 15);
}
void displayFcn (void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
//glPolygonMode(GL_FRONT, GL_FILL);
//glPolygonMode(GL_BACK, GL_FILL);
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(0.5, 1.0, 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();
}
This is the full source code, you can copy and paste to your VS solution and compile.
You'll need to have glut installed.
The result comes up like this:
The center of the window is 0,0 in opengl. So, when you calculate the vertices, you have to calculate them such that the center of the triangle is 0,0.
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(0.5, 1.0, 0.0);
glEnd();
those coords will need to be updated, you can find a discussion on finding the centers of triangles at this question: finding center of 2D triangle which sounds like it's from a similar homework assignment.
glTranslatef(-0.5f, -0.5f, 0.0f);
The default perspective for OpenGL is the origin centered and the window x and y ranging from -1 to 1. You can either change this by changing the default viewing volume or changing the coordinates of your triangle.
Either
glTranslatef(-.5f, -.5f, .0f);
or
glBegin(GL_TRIANGLES);
glVertex3f(-.5, -.5, 0.0);
glVertex3f(.50, -.5, 0.0);
glVertex3f(0, .5, 0.0);
glEnd();
will work.
It looks like you're trying to use glFrustum where you want to use glOrtho
glFrustum is supposed to be used for generating a perspective matrix. zNear is never 0. The way you call it right now generates an error GL_INVALID_VALUE, so you get the default projection matrix, the identity.
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();
}