0
\$\begingroup\$

I am new to OpenGL and am learning it for the purpose of some simulation that I am doing as part of my research work. I have been following the tutorials given at http://openglbook.com/chapter-2-vertices-and-shapes.html and am using the code which draws a triangle. I am trying to add some code to it so that I can rotate the triangle using mouse upon display. I am using glTranslatef and glRotatef to achieve this. I understand that glrotate takes 4 arguments first being the angle of rotation, the last three form the vector centred at origin around which rotation is intended. I am printing captured values from my motion func and the mouse input is being captured. But my triangle is not rotating. Any help will be much appreciated. Thanks! Code below.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#define WINDOW_TITLE_PREFIX "Chapter 2"

int
 CurrentWidth = 800,
 CurrentHeight = 800,
 WindowHandle = 0;

unsigned FrameCount = 0;

GLuint
  VertexShaderId,
  FragmentShaderId,
  ProgramId,
  VaoId,
  VboId,
  ColorBufferId;

 // 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;

const GLchar* VertexShader =
{
  "#version 400\n"\

  "layout(location=0) in vec4 in_Position;\n"\
  "layout(location=1) in vec4 in_Color;\n"\
  "out vec4 ex_Color;\n"\

  "void main(void)\n"\
  "{\n"\
  "  gl_Position = in_Position;\n"\
  "  ex_Color = in_Color;\n"\
  "}\n"
};

const GLchar* FragmentShader =
{
  "#version 400\n"\

 "in vec4 ex_Color;\n"\
 "out vec4 out_Color;\n"\

 "void main(void)\n"\
 "{\n"\
 "  out_Color = ex_Color;\n"\
 "}\n"
};

void Initialize(int, char*[]);
void InitWindow(int, char*[]);
void ResizeFunction(int, int);
void RenderFunction(void);
void TimerFunction(int);
void IdleFunction(void);
void Cleanup(void);
void CreateVBO(void);
void DestroyVBO(void);
void CreateShaders(void);
void DestroyShaders(void);
//Mouse Movements//
void mouse(int button, int state, int x, int y);
void motion(int x, int y);

int main(int argc, char* argv[])
{
  Initialize(argc, argv);

  glutMainLoop();

  exit(EXIT_SUCCESS);
}

void Initialize(int argc, char* argv[])
{
  GLenum GlewInitResult;

  InitWindow(argc, argv);

  glewExperimental = GL_TRUE;
  GlewInitResult = glewInit();

  if (GLEW_OK != GlewInitResult) {
    fprintf(
    stderr,
   "ERROR: %s\n",
    glewGetErrorString(GlewInitResult)
    );
   exit(EXIT_FAILURE);
  }

  fprintf(
  stdout,
  "INFO: OpenGL Version: %s\n",
   glGetString(GL_VERSION)
 );

 CreateShaders();
 CreateVBO();

 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
 }

void InitWindow(int argc, char* argv[])
{
   glutInit(&argc, argv);

   glutInitContextVersion(4, 0);
   glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
   glutInitContextProfile(GLUT_CORE_PROFILE);

   glutSetOption(
     GLUT_ACTION_ON_WINDOW_CLOSE,
     GLUT_ACTION_GLUTMAINLOOP_RETURNS
  );

    glutInitWindowSize(CurrentWidth, CurrentHeight);

   glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

   WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);

  if(WindowHandle < 1) {
     fprintf(
     stderr,
     "ERROR: Could not create a new rendering window.\n"
    );
    exit(EXIT_FAILURE);
   }

  glutReshapeFunc(ResizeFunction);
  glutDisplayFunc(RenderFunction);  
  glutMouseFunc(mouse);
  glutMotionFunc(motion);
  glutIdleFunc(IdleFunction);
  glutTimerFunc(0, TimerFunction, 0);
  glutCloseFunc(Cleanup);
 }

 void ResizeFunction(int Width, int Height)
 {
  CurrentWidth = Width;
  CurrentHeight = Height;
  glViewport(0, 0, CurrentWidth, CurrentHeight);
 }

  void RenderFunction(void)
  {
    ++FrameCount;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   // set view matrix
   glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
   glTranslatef(0.0, 0.0, translate_z);
     printf ("translate_x = %f rotate_x = %f rotate_y = %f  \n",rotate_x,rotate_y,translate_z);
   glRotatef(rotate_x, 1.0, 0.0, 0.0);
   glRotatef(rotate_y, 0.0, 1.0, 0.0);
  glDrawArrays(GL_TRIANGLES, 0, 3);
  glutSwapBuffers();
 }

  void IdleFunction(void)
 {
    glutPostRedisplay();
  }

 void TimerFunction(int Value)
{
  if (0 != Value) {
   char* TempString = (char*)
    malloc(512 + strlen(WINDOW_TITLE_PREFIX));

sprintf(
  TempString,
  "%s: %d Frames Per Second @ %d x %d",
  WINDOW_TITLE_PREFIX,
  FrameCount * 4,
  CurrentWidth,
    CurrentHeight
  );

glutSetWindowTitle(TempString);
free(TempString);
 }

  FrameCount = 0;
  glutTimerFunc(250, TimerFunction, 1);
  }

void Cleanup(void)
{
     DestroyShaders();
     DestroyVBO();
}

void CreateVBO(void)
{
  GLfloat Vertices[] = {
    -0.8f, -0.8f, 0.0f, 1.0f,
    0.0f,  0.8f, 0.0f, 1.0f,
     0.8f, -0.8f, 0.0f, 1.0f
   };

 GLfloat Colors[] = {
   1.0f, 0.0f, 0.0f, 1.0f,
    0.0f, 1.0f, 0.0f, 1.0f,
   0.0f, 0.0f, 1.0f, 1.0f
  };

GLenum ErrorCheckValue = glGetError();

glGenVertexArrays(1, &VaoId);
glBindVertexArray(VaoId);

 glGenBuffers(1, &VboId);
 glBindBuffer(GL_ARRAY_BUFFER, VboId);
 glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
 glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
 glEnableVertexAttribArray(0);

 glGenBuffers(1, &ColorBufferId);
 glBindBuffer(GL_ARRAY_BUFFER, ColorBufferId);
glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);

 ErrorCheckValue = glGetError();
 if (ErrorCheckValue != GL_NO_ERROR)
 {
   fprintf(
     stderr,
     "ERROR: Could not create a VBO: %s \n",
     gluErrorString(ErrorCheckValue)
   );

   exit(-1);
 }
}

void DestroyVBO(void)
 {
  GLenum ErrorCheckValue = glGetError();

 glDisableVertexAttribArray(1);
 glDisableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);

 glDeleteBuffers(1, &ColorBufferId);
 glDeleteBuffers(1, &VboId);

 glBindVertexArray(0);
  glDeleteVertexArrays(1, &VaoId);

  ErrorCheckValue = glGetError();
 if (ErrorCheckValue != GL_NO_ERROR)
 {
   fprintf(
     stderr,
     "ERROR: Could not destroy the VBO: %s \n",
     gluErrorString(ErrorCheckValue)
   );

   exit(-1);
  }
}

void CreateShaders(void)
{
  GLenum ErrorCheckValue = glGetError();

  VertexShaderId = glCreateShader(GL_VERTEX_SHADER);
  glShaderSource(VertexShaderId, 1, &VertexShader, NULL);
  glCompileShader(VertexShaderId);

  FragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
 glShaderSource(FragmentShaderId, 1, &FragmentShader, NULL);
   glCompileShader(FragmentShaderId);

  ProgramId = glCreateProgram();
   glAttachShader(ProgramId, VertexShaderId);
   glAttachShader(ProgramId, FragmentShaderId);
   glLinkProgram(ProgramId);
 glUseProgram(ProgramId);

  ErrorCheckValue = glGetError();
  if (ErrorCheckValue != GL_NO_ERROR)
 {
    fprintf(
      stderr,
      "ERROR: Could not create the shaders: %s \n",
      gluErrorString(ErrorCheckValue)
    );

   exit(-1);
   }
 }

 void DestroyShaders(void)
 {
   GLenum ErrorCheckValue = glGetError();

  glUseProgram(0);

    glDetachShader(ProgramId, VertexShaderId);
   glDetachShader(ProgramId, FragmentShaderId);

   glDeleteShader(FragmentShaderId);
  glDeleteShader(VertexShaderId);

  glDeleteProgram(ProgramId);

   ErrorCheckValue = glGetError();
   if (ErrorCheckValue != GL_NO_ERROR)
  {
    fprintf(
      stderr,
     "ERROR: Could not destroy the shaders: %s \n",
      gluErrorString(ErrorCheckValue)
   );

   exit(-1);
  }
}

  ////////////////////////////////////////////////////////////////////////////////
  //! Keyboard events handler
  ////////////////////////////////////////////////////////////////////////////////
 void KeyboardFunction(unsigned char key, int X, int Y)
 {
switch (key)
{
    case (27) :
        exit(EXIT_SUCCESS);
        break;
  }
}

  ////////////////////////////////////////////////////////////////////////////////
  //! Mouse event handlers
  ////////////////////////////////////////////////////////////////////////////////
void mouse(int button, int state, int x, int y)
{
 if (state == GLUT_DOWN)
 {
     mouse_buttons |= 1<<button;
 printf("Mouse Button was pressed \n");
}
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;printf("ROTATE value is being set \n");
}
else if (mouse_buttons & 4)
{
    translate_z += dy * 0.01f;printf("TRANSLATE is being set \n");
}

mouse_old_x = x;
mouse_old_y = y;
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to gamedev stack exchange! As a small suggestion, when you submit code, you should try and isolate the part that you think is not working instead of submitting the whole thing. You'll be more likely to receive an answer :) \$\endgroup\$ – Vaillancourt May 24 '15 at 12:04
  • \$\begingroup\$ Alexandre - thank you, I am new to all this but I will keep that in mind in future questions. :) \$\endgroup\$ – Sourav May 24 '15 at 17:08
  • \$\begingroup\$ @Sourav you can edit this question to simplify your code to a minimal example. Although in your case, you have posted your complete program, so it's really just a matter of helping potential answers find the important part. For example, your RenderFunction where you are attempting to rotate: perhaps include additional description of that. \$\endgroup\$ – Seth Battin May 28 '15 at 14:02
2
\$\begingroup\$

glRotatef and glTranslatef are old functions meant for use with the fixed-function pipeline, so they don't work with opengl 4 shaders. Instead you need to create a matrix, send it to your shader, and multiply your vertices by it when setting gl_Position.

The exact process will probably be covered in one of the following tutorials on that site, so I'd recommend just continuing with them.

\$\endgroup\$
  • \$\begingroup\$ Thank you ,this helped. I guess i jumped ahead of myself in the tutorial and referred some out of date tutorial in the process. \$\endgroup\$ – Sourav May 24 '15 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.