OpenGL Not Drawing Anything - c++

Just trying to draw a line between two points using glut for openGL version 3.0. Here's the code.
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
void init(void) {
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,30.0,0.0,30.0);
}
void setPixel(GLint x, GLint y) {
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,30.0,0.0,30.0);
}
float roundValue(float v) {
return floor(v + 0.5);
}
void lineDDA(int xa, int ya, int xb, int yb) {
double dx = xb-xa, dy = yb-ya, steps;
float xInc, yInc, x = xa, y = ya;
if (abs(dx) > abs(dy))
steps = abs(dx);
else
steps = abs(dy);
xInc = dx/(float)steps;
yInc = dy/(float)steps;
setPixel(x, y);
int k;
for (k = 0; k < steps; ++k) {
/* code */
x += xInc;
y += yInc;
setPixel(roundValue(x), roundValue(y));
}
}
void update() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(5.0f);
glColor3f(1, 0, 0);
lineDDA(1, 1, 8, 7);
lineDDA(1, 1, 8, 2);
glFlush();
}
int main(int argc,char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(200,100);
glutInitWindowSize(640,480);
glutCreateWindow("Tutorial-1: Q4");
glLoadIdentity();
init();
glutDisplayFunc(update);
glutMainLoop();
return 0;
}
But this is not drawing anything on the window. It just remains white. Can anyone please suggest a solution?

Your setPixel function doesn't use its parameters x and y. You are not enabling line rendering mode or adding vertices to be drawn.
Fixing would consist of at least these steps:
Call glBegin(GL_LINES); after glPointSize(5.0f);
Call glEnd() before glFlush();
Modify setPixel to look like this:
void setPixel(GLint x, GLint y)
{
glVertex2f(x, y);
}

Related

C++ OpenGL Draw Pixel

I'm learning how to use OpenGL by doing an online course and the practice problem asks to create a function to draw pixels:
void SetPixel(int x, int y, char r, char g, char b){}
I know I could do something like this to draw one:
glBegin(GL_POINTS);
glColor3f(1.0,1.0,1.0);
glVertex2i(100,100);
glEnd();
However given the context of the problem it's not a valid solution.
The problem also asks to use the SetPixel function by calling it within a loop to randomly place pixels across the screen. I understand how to implement that, but it's the drawing the actual pixel I don't understand, especially within the context of the program given.
The program (using GLUT):
#include <stdlib.h> //- for exit()
#include <stdio.h> //- for sprintf()
#include <string.h> //- for memset()
#ifdef _WIN32
#include "libs/glut.h"
#include <windows.h>
#pragma comment(lib, "winmm.lib") //- not required but have it in anyway
#pragma comment(lib, "libs/glut32.lib")
#elif __APPLE__
#include <GLUT/glut.h>
#elif __unix__
#include <GL/glut.h>
#endif
#define FRAME_WIDE 1000
#define FRAME_HIGH 600
//====== Structs & typedefs =========
typedef unsigned char BYTE;
struct POINT2D {int x, y;};
//====== Global Variables ==========
BYTE pFrameL[FRAME_WIDE * FRAME_HIGH * 3];
BYTE pFrameR[FRAME_WIDE * FRAME_HIGH * 3];
int shade = 0;
POINT2D xypos = {0,0};
int stereo = 0;
int eyes = 10;
//===== Forward Declarations ========
void ClearScreen();
void DrawFrame();
void Interlace(BYTE* pL, BYTE* pR);
void BuildFrame(BYTE *pFrame, int view);
void OnIdle(void);
void OnDisplay(void);
void reshape(int w, int h);
////////////////////////////////////////////////////////
// Program Entry Poinr
////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
//-- setup GLUT --
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); //GLUT_3_2_CORE_PROFILE |
glutInitWindowSize(FRAME_WIDE, FRAME_HIGH);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
//--- set openGL state --
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//-- register call back functions --
glutIdleFunc(OnIdle);
glutDisplayFunc(OnDisplay);
glutReshapeFunc(reshape);
//-- run the program
glutMainLoop();
return 0;
}
////////////////////////////////////////////////////////
// Event Handers
////////////////////////////////////////////////////////
void OnIdle(void)
{
DrawFrame();
glutPostRedisplay();
}
void OnDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0, 0);
glDrawPixels(FRAME_WIDE, FRAME_HIGH, GL_RGB,GL_UNSIGNED_BYTE, (GLubyte*)pFrameR);
glutSwapBuffers();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
////////////////////////////////////////////////////////
// Utility Functions
////////////////////////////////////////////////////////
void ClearScreen()
{
memset(pFrameL, 0, FRAME_WIDE * FRAME_HIGH * 3);
memset(pFrameR, 0, FRAME_WIDE * FRAME_HIGH * 3);
}
void Interlace(BYTE* pL, BYTE* pR)
{
int rowlen = 3 * FRAME_WIDE;
for (int y = 0; y < FRAME_HIGH; y+=2)
{
for (int x = 0; x < rowlen; x++) *pR++ = *pL++;
pL += rowlen;
pR += rowlen;
}
}
void DrawFrame()
{
ClearScreen();
if (!stereo) BuildFrame(pFrameR, 0);
else {
BuildFrame(pFrameL, -eyes);
BuildFrame(pFrameR, +eyes);
Interlace((BYTE*)pFrameL, (BYTE*)pFrameR);
}
}
////////////////////////////////////////////////////////
// Drawing Function
////////////////////////////////////////////////////////
void BuildFrame(BYTE *pFrame, int view)
{
// create a loop in here that uses SetPixel function to randomly place 10,000 pixels
}
void SetPixel(int x, int y, char r, char g, char b)
{
}
Can someone please help me work this out? How would I implement the SetPixel function elegantly?
Based on your configurations for the pixel format, the red component of the pixel at (x, y) is at index 3 * (y * width + x), and +1 / 2 for green / blue:
void SetPixel(int x, int y, char r, char g, char b)
{
if (x < 0 || x >= FRAME_WIDE || y < 0 || y >= FRAME_HIGH)
return; // out of bounds
int index = 3 * (y * FRAME_WIDE + x);
pFrame[index] = r;
pFrame[index+1] = g;
pFrame[index+2] = b;
}
Note that it is unclear whether SetPixel should write both buffers or there should be a flag indicating which one, so I just wrote pFrame as a placeholder.

glutKeyboardUpFunc get's called without key release

Im having a problem with glutKeyboardUpFunc. Everytime I press a key and don't release it, the callback of glutKeyboardUpFunc stills gets called, the longer I hold the key the more times the callback executes.
Here is a quick example I put up:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <time.h>
#include <GL/glut.h>
#define DEBUG 1
/* VARI�VEIS GLOBAIS */
typedef struct {
GLboolean doubleBuffer;
GLint delay;
} Estado;
typedef struct {
GLfloat x;
GLfloat y;
GLfloat height;
GLfloat width;
GLfloat y_accelaration;
} Plataforma;
//save stage coordinates
typedef struct {
GLfloat top;
GLfloat bottom;
GLfloat right;
GLfloat left;
} Screen;
Estado estado;
Screen ecra;
Plataforma plataforma;
GLfloat tab_speed = 2.0 / 20.0;
GLfloat y_accelaration = 0.001;
void Init(void) {
struct tm *current_time;
time_t timer = time(0);
estado.delay = 42;
estado.doubleBuffer = GL_TRUE;
plataforma.height = 0.3;
plataforma.width = 0.05;
plataforma.y_accelaration = 0;
plataforma.y = 0;
// L� hora do Sistema
current_time = localtime(&timer);
glClearColor(0.3, 0.3, 0.3, 0.0);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
}
void Reshape(int width, int height) {
GLint size;
GLfloat ratio = (GLfloat) width / height;
GLfloat ratio1 = (GLfloat) height / width;
if (width < height)
size = width;
else
size = height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (width < height) {
ecra.left = -1;
ecra.right = 1;
ecra.bottom = -1 * ratio1;
ecra.top = 1 * ratio1;
gluOrtho2D(-1, 1, -1 * ratio1, 1 * ratio1);
} else {
ecra.left = -1 * ratio;
ecra.right = 1 * ratio;
ecra.bottom = -1;
ecra.top = 1;
gluOrtho2D(-1 * ratio, 1 * ratio, -1, 1);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void desenhar_plataforma(Plataforma pf) {
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(pf.x - pf.width / 2, pf.y - pf.height / 2);
glVertex2f(pf.x + pf.width / 2, pf.y - pf.height / 2);
glVertex2f(pf.x + pf.width / 2, pf.y + pf.height / 2);
glVertex2f(pf.x - pf.width / 2, pf.y + pf.height / 2);
glEnd();
}
void Draw(void) {
glClear(GL_COLOR_BUFFER_BIT);
plataforma.x = ecra.left + 0.1;
desenhar_plataforma(plataforma);
glFlush();
if (estado.doubleBuffer)
glutSwapBuffers();
}
void Key(unsigned char key, int x, int y) {
switch (key) {
case 'a':
plataforma.y += tab_speed;
glutPostRedisplay();
break;
case 's':
plataforma.y -= tab_speed;
glutPostRedisplay();
break;
}
}
void KeyUp(unsigned char key, int x, int y) {
if (DEBUG)
printf("Key Up %c\n", key);
}
int main(int argc, char **argv) {
estado.doubleBuffer = GL_TRUE;
glutInit(&argc, argv);
glutInitWindowPosition(0, 0);
glutInitWindowSize(800, 600);
glutInitDisplayMode(((estado.doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE) | GLUT_RGB);
if (glutCreateWindow("No Man's Pong") == GL_FALSE)
exit(1);
Init();
glutReshapeFunc(Reshape);
glutDisplayFunc(Draw);
// Callbacks de teclado
glutKeyboardFunc(Key);
glutKeyboardUpFunc(KeyUp);
glutMainLoop();
return 0;
}
Here's the ouput when pressing 'a':
Key Up a
Key Up a
Key Up a
Key Up a
Key Up a
Key Up a
Key Up a
......
Needless to say im not releasing 'a'.
Im using linux mint 17.3 and g++, shutting off key repeat or setting glutIgnoreKeyRepeat(true), stops this issue, but also makes glutKeyboardFunc execute only once per key press, and I realy don't want that.
Does anyone know how to fix this, or whats causing this?
I also compiled the same code under windows 8.1 and the keyUp only fires when there's an actual key release.

I am trying to use structure in c++ but its not working

in this method
void Sierpinski(GLintPoint points) {
glClear(GL_COLOR_BUFFER_BIT);
GLintPoint T[3] = {{10,10},{600,10},{300,600}};
int index=rand()%3;
GLintPoint point=points[index];
drawDot(point.x, point.y);
for (int i = 0;i<55000;i++) {
index=rand()%3;
point.x=(point.x+points[index].x)/2;
point.y=(point.y+points[index].y)/2;
drawDot(point.x,point.y);
}
glFlush();
}
that uses the structure i made
struct GLintPoint {
GLint x,y;
};
it says that there is an error and that "no operator "[]" matches these operands, operator types are GLintPoint[int]" where i try to assign a value from points to point. Well i did use the right brackets and it is an int in there so what is the problem? FYI this code is to draw the Sierpinski gasket with the user making the initial 3 points by clicking the screen with the mouse. Just in case you would like to see it here is the whole program.
#include <windows.h>
#include <gl/Gl.h>
#include "glut.h"
#include <iostream>
using namespace std;
const int screenWidth = 640;
const int screenHeight = 480;
struct GLintPoint {
GLint x,y;
};
void display (void){
glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1.0,1.0,1.0);
glFlush();
}
void drawDot( GLint x, GLint y)
{
glBegin( GL_POINTS );
glVertex2i( x, y );
glEnd();
}
void Sierpinski(GLintPoint points) {
glClear(GL_COLOR_BUFFER_BIT);
GLintPoint T[3] = {{10,10},{600,10},{300,600}};
int index=rand()%3;
GLintPoint point=points[index];
drawDot(point.x, point.y);
for (int i = 0;i<55000;i++) {
index=rand()%3;
point.x=(point.x+points[index].x)/2;
point.y=(point.y+points[index].y)/2;
drawDot(point.x,point.y);
}
glFlush();
}
void myMouse(int button, int state, int x, int y){
static GLintPoint corner[2];
static int numCorners = 0;
if(state == GLUT_DOWN){
if(button == GLUT_LEFT_BUTTON){
corner[numCorners].x = x;
corner[numCorners].y = screenHeight - y;
if(++numCorners ==2 ){
glRecti(corner[0].x, corner[0].y, corner[1].x, corner[1].y);
numCorners = 0;
glFlush();
}
}
else if(button == GLUT_RIGHT_BUTTON){
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
}
}
void myInit() {
glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
}
void main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100,150);
glutCreateWindow("mouse dots");
glutDisplayFunc(display);
glutPostRedisplay();
glutMouseFunc(myMouse);
myInit();
glutMainLoop();
}
I'm inferring from the name of the function argument, points, that you intend the function to accept an array of points. But it's actually just written to take one.
So when you write points[index] the compiler is looking for operator[] in your GLintPoint struct (and there isn't one).
If you change the function prototype to take an array of GLintPoints I think you'll have better luck.

OpenGL Glut Drawing Points Not Working

Can someone help me out in checking my code? I'm trying to draw a simple dot by clicking in the window, but I can't see any points drawn. The point recording system is copied as an example from one of the stack overflow posts.
#include <stdlib.h>
#include <stdio.h>
//#include <GL\glew.h>
#include <gl/glut.h>
//#include <GL\freeglut.h>
#include <math.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "draw.h"
//DRAW draw2;
class Point
{
int Xvalue, Yvalue;
std::string Text;
public:
void xy(int x, int y)
{
Xvalue = x;
Yvalue = y;
}
void text(std::string text)
{
Text = text;
}
//return individual x y value
int x() { return Xvalue; }
int y() { return Yvalue; }
//return text value
std::string rtext() { return Text; }
};
Point point[30];
int count = 0;
void Init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
//glColor3f(0.0, 0.0, 0.0);
//glPointSize(5);
glMatrixMode(GL_PROJECTION); //coordinate system
//glLoadIdentity();
gluOrtho2D(0.0, 1200.0, 0.0, 800.0);
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT); // clear display window
glColor3f(1.0, 1.0, 1.0);
//glLoadIdentity();
/*Drawing here*/
//redraw
for (int i = 0; i < count; i++)
{
int x = point[i].x();
int y = point[i].y();
//draw2.Dot(x, y);
std::cout << "drawing dot " << x << " " << y << std::endl;
glBegin(GL_POINTS);
glColor3f(0.3, 0.3, 0.3);
glPointSize(5.0f);
glVertex2i(x, glutGet(GLUT_WINDOW_HEIGHT) - y);
glEnd();
}
glFlush();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
point[count].xy(x, y);
//draw2.Dot(x, y);
count++;
}
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv); //initialize toolkit
//Request double buffered true color window with Z-buffer
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB ); //display mode
glutInitWindowSize(1200, 800); //set window size
glutCreateWindow("NURBS Curve"); //open screen window
Init(); //additional initializations
//CALLBACK FUNCTIONS
glutMouseFunc(mouse);
glutDisplayFunc(Display); //send graphics to display window
/*
GLenum err = glewInit();
if (GLEW_OK != err)
{
fprintf(stderr, "GLEW error");
return 1;
}
*/
//pass control to glut for events, loops
glutMainLoop();
return 0;
}
glBegin(GL_POINTS);
glColor3f(0.3, 0.3, 0.3);
glPointSize(5.0f); // wat
glVertex2i(x, glutGet(GLUT_WINDOW_HEIGHT) - y);
glEnd();
There's a short list of GL functions you can call inside a glBegin()/glEnd() pair and glPointSize() is not on it:
Only a subset of GL commands can be used between glBegin and glEnd.
The commands are
glVertex,
glColor,
glSecondaryColor,
glIndex,
glNormal,
glFogCoord,
glTexCoord,
glMultiTexCoord,
glVertexAttrib,
glEvalCoord,
glEvalPoint,
glArrayElement,
glMaterial, and
glEdgeFlag.
Also,
it is acceptable to use
glCallList or
glCallLists to execute
display lists that include only the preceding commands.
If any other GL command is executed between glBegin and glEnd,
the error flag is set and the command is ignored.
Move the glPointSize() outside the glBegin()/glEnd() pair:
glPointSize(5.0f);
glBegin(GL_POINTS);
for (int i = 0; i < count; i++)
{
int x = point[i].x();
int y = point[i].y();
glColor3f(0.3, 0.3, 0.3);
glVertex2i(x, h - y);
}
glEnd();
All together:
#include <gl/glut.h>
class Point
{
int Xvalue, Yvalue;
public:
void xy(int x, int y)
{
Xvalue = x;
Yvalue = y;
}
//return individual x y value
int x() { return Xvalue; }
int y() { return Yvalue; }
};
Point point[30];
int count = 0;
void Display()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT); // clear display window
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
const double w = glutGet( GLUT_WINDOW_WIDTH );
const double h = glutGet( GLUT_WINDOW_HEIGHT );
gluOrtho2D(0.0, w, 0.0, h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0, 1.0, 1.0);
glPointSize(5.0f);
glBegin(GL_POINTS);
for (int i = 0; i < count; i++)
{
int x = point[i].x();
int y = point[i].y();
glColor3f(0.3, 0.3, 0.3);
glVertex2i(x, h - y);
}
glEnd();
glFlush();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
point[count].xy(x, y);
count++;
}
glutPostRedisplay();
}
int main( int argc, char *argv[] )
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(1200, 800);
glutCreateWindow("NURBS Curve");
glutMouseFunc(mouse);
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}
Found out that glcolor3f and glpointsize had to be initialized first in my Init() function.
void Init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0); //this one
glPointSize(5); //and this one
glMatrixMode(GL_PROJECTION); //coordinate system
//glLoadIdentity();
gluOrtho2D(0.0, 1200.0, 0.0, 800.0);
}

opengl not drawing anything

Just trying to draw a point using glut and glew for opengl version 4.3
my code
void Renderer(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glPointSize(100);
glColor3f(255, 0, 0);
glVertex3d(10, 10, 0);
glEnd();
glFlush();
glutSwapBuffers();
}
is not rendering anything, can someone tell me what did i miss? here is full code:
#include <iostream>
using namespace std;
#include "vgl.h"
#include "LoadShaders.h"
enum VAO_IDs { Triangles, NumVAOS };
#define WIDTH 1024
#define HEIGHT 768
#define REFRESH_DELAY 10 //ms
//general
long frameCount = 0;
// mouse controls
int mouse_old_x, mouse_old_y;
int mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0;
float translate_z = -3.0;
/////////////////////////////////////////////////////////////////////
//! Prototypes
/////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void motion(int x, int y);
void timerEvent(int value)
{
glutPostRedisplay();
glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount++);
}
void init (void)
{
// default initialization
glClearColor(0.0, 0.0, 0.0, 1.0);
glDisable(GL_DEPTH_TEST);
// viewport
glViewport(0, 0, WIDTH, HEIGHT);
// projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)WIDTH / (GLfloat)HEIGHT, 1, 10000.0);
}
void Renderer(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glPointSize(100);
glColor3f(255, 0, 0);
glVertex3d(10, 10, 0);
glEnd();
glFlush();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow("The Abyss");
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount);
if (glewInit()) //i guess this is true on failure
{
cerr << "Error initializing glew, Program aborted." << endl;
exit(EXIT_FAILURE);
}
//Init First
init();
//Init callback for Rendering
glutDisplayFunc(Renderer);
//Main Loop
glutMainLoop();
exit(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////
//!Mouse and keyboard functionality
////////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int /*x*/, int /*y*/)
{
switch (key)
{
case (27) :
exit(EXIT_SUCCESS);
break;
}
}
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
mouse_buttons |= 1<<button;
}
else if (state == GLUT_UP)
{
mouse_buttons = 0;
}
mouse_old_x = x;
mouse_old_y = y;
}
void motion(int x, int y)
{
float dx, dy;
dx = (float)(x - mouse_old_x);
dy = (float)(y - mouse_old_y);
if (mouse_buttons & 1)
{
rotate_x += dy * 0.2f;
rotate_y += dx * 0.2f;
}
else if (mouse_buttons & 4)
{
translate_z += dy * 0.01f;
}
mouse_old_x = x;
mouse_old_y = y;
}
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
You have requested a Core context. You need to:
Specify a vertex and fragment shader. There are no freebies in Core.
Stop using deprecated functionality like glBegin() and glMatrixMode().
Start using VBOs to submit your geometry.
Start using glDrawArrays() and friends to draw your geometry.